标签:lambda print 资源 exception 相等 public 系统资源 code static
死锁是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一一种==互相等待的现象==,若无外力干涉那它们都将无法推进下去,如果系统资源充足,进程的资源请求都能够得到满足,死锁出现的可能性就很低,否则就会因争夺有限的资源而陷入死锁。
public class DeadLock {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
new Thread(()->{
synchronized (o1) {
System.out.println(Thread.currentThread().getName()+"\t 我抢到了o1");
try {
TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
synchronized (o2) {
System.out.println(Thread.currentThread().getName()+"\t 我抢到了o2");
}
}
},"A").start();
new Thread(()->{
synchronized (o2) {
System.out.println(Thread.currentThread().getName()+"\t 我抢到了o2");
try {
TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
synchronized (o1) {
System.out.println(Thread.currentThread().getName()+"\t 我抢到了o1");
}
}
},"B").start();
}
}
jps -l 查看当前java运行的线程
18704 sun.tools.jps.Jps
13044 org.jetbrains.kotlin.daemon.KotlinCompileDaemon
10616 尚硅谷.juc.锁篇.死锁.DeadLock
20712 org.jetbrains.jps.cmdline.Launcher
23036 org.jetbrains.idea.maven.server.RemoteMavenServer36
5804
jstack 进程号
Java stack information for the threads listed above:
===================================================
"B":
at 尚硅谷.juc.锁篇.死锁.DeadLock.lambda$main$1(DeadLock.java:33)
- waiting to lock <0x00000000d621ba58> (a java.lang.Object)
- locked <0x00000000d621ba68> (a java.lang.Object)
at 尚硅谷.juc.锁篇.死锁.DeadLock$$Lambda$2/2093631819.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"A":
at 尚硅谷.juc.锁篇.死锁.DeadLock.lambda$main$0(DeadLock.java:22)
- waiting to lock <0x00000000d621ba68> (a java.lang.Object)
- locked <0x00000000d621ba58> (a java.lang.Object)
at 尚硅谷.juc.锁篇.死锁.DeadLock$$Lambda$1/558638686.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
标签:lambda print 资源 exception 相等 public 系统资源 code static
原文地址:https://www.cnblogs.com/lovestart/p/12464750.html