Java教程

JAVA多线程-死锁

本文主要是介绍JAVA多线程-死锁,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.code demo

点击查看代码
public class DemoThread12 {

	private Object lock1 = new Object();
	private Object lock2 = new Object();

	public void execute1() {
		synchronized (lock1) {
			System.out.println("线程" + Thread.currentThread().getName() + "获得lock1执行execute1开始");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (lock2) {
				System.out.println("线程" + Thread.currentThread().getName() + "获得lock2执行execute1开始");
			}
		}
	}

	public void execute2() {
		synchronized (lock2) {
			System.out.println("线程" + Thread.currentThread().getName() + "获得lock2执行execute2开始");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (lock1) {
				System.out.println("线程" + Thread.currentThread().getName() + "获得lock1执行execute2开始");
			}
		}
	}

	public static void main(String[] args) {
		final DemoThread12 demo = new DemoThread12();
		new Thread(new Runnable() {
			@Override
			public void run() {
				demo.execute1();
			}
		}, "t1").start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				demo.execute2();
			}
		}, "t2").start();
	}
}

 

2.查看进程号:jps 查看java进程

 

3.查看线程运行状态:jstack 20040

点击查看代码
E:\MideaWorkSpaceDemo\JavaDemo>jstack 20040
2021-12-01 21:12:06
Full thread dump OpenJDK Client VM (25.40-b25 mixed mode):

"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x031bd000 nid=0x2c50 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"t2" #12 prio=5 os_prio=0 tid=0x15d8b400 nid=0x48e0 waiting for monitor entry [0x1648f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at ThreadDemo.ThreadExamp.base.DemoThread12.execute2(DemoThread12.java:50)
        - waiting to lock <0x05a1abc8> (a java.lang.Object)
        - locked <0x05a1abd0> (a java.lang.Object)
        at ThreadDemo.ThreadExamp.base.DemoThread12$2.run(DemoThread12.java:67)
        at java.lang.Thread.run(Thread.java:745)

"t1" #11 prio=5 os_prio=0 tid=0x15d84400 nid=0x414c waiting for monitor entry [0x163ff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at ThreadDemo.ThreadExamp.base.DemoThread12.execute1(DemoThread12.java:36)
        - waiting to lock <0x05a1abd0> (a java.lang.Object)
        - locked <0x05a1abc8> (a java.lang.Object)
        at ThreadDemo.ThreadExamp.base.DemoThread12$1.run(DemoThread12.java:60)
        at java.lang.Thread.run(Thread.java:745)

"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x15d46800 nid=0x4e88 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread0" #9 daemon prio=9 os_prio=2 tid=0x15d06000 nid=0x34dc waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"JDWP Command Reader" #8 daemon prio=10 os_prio=0 tid=0x15ccb000 nid=0x4720 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"JDWP Event Helper Thread" #7 daemon prio=10 os_prio=0 tid=0x15cc8000 nid=0x3794 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"JDWP Transport Listener: dt_socket" #6 daemon prio=10 os_prio=0 tid=0x15c97000 nid=0x2a38 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x15c8cc00 nid=0x43c0 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x15cc1800 nid=0x3e10 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x15c66000 nid=0x1d68 in Object.wait() [0x15eef000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x058062c0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x058062c0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x15c61400 nid=0x3a7c in Object.wait() [0x057ef000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x05805db8> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
        - locked <0x05805db8> (a java.lang.ref.Reference$Lock)

"VM Thread" os_prio=2 tid=0x0359cc00 nid=0x468c runnable

"VM Periodic Task Thread" os_prio=2 tid=0x15d47800 nid=0x51c8 waiting on condition

JNI global references: 1514


Found one Java-level deadlock:
=============================
"t2":
  waiting to lock monitor 0x15c65df4 (object 0x05a1abc8, a java.lang.Object),
  which is held by "t1"
"t1":
  waiting to lock monitor 0x15c64264 (object 0x05a1abd0, a java.lang.Object),
  which is held by "t2"

Java stack information for the threads listed above:
===================================================
"t2":
        at ThreadDemo.ThreadExamp.base.DemoThread12.execute2(DemoThread12.java:50)
        - waiting to lock <0x05a1abc8> (a java.lang.Object)
        - locked <0x05a1abd0> (a java.lang.Object)
        at ThreadDemo.ThreadExamp.base.DemoThread12$2.run(DemoThread12.java:67)
        at java.lang.Thread.run(Thread.java:745)
"t1":
        at ThreadDemo.ThreadExamp.base.DemoThread12.execute1(DemoThread12.java:36)
        - waiting to lock <0x05a1abd0> (a java.lang.Object)
        - locked <0x05a1abc8> (a java.lang.Object)
        at ThreadDemo.ThreadExamp.base.DemoThread12$1.run(DemoThread12.java:60)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.
这篇关于JAVA多线程-死锁的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!