java写一段死锁代码
作者:
小明17 | 来源:发表于
2019-06-04 21:15 被阅读0次public class Solution{
static Object o1 = new Object();
static Object o2 = new Object();
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (Solution.o1){
System.out.println("thread 1 get o1");
try {
Thread.sleep(100);
synchronized (Solution.o2){
System.out.println("thread1 get o2");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (Solution.o2){
System.out.println("thread 2 get o2");
try {
Thread.sleep(100);
synchronized (Solution.o1){
System.out.println("thread2 get o1");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
本文标题:java写一段死锁代码
本文链接:https://www.haomeiwen.com/subject/ygtdxctx.html
网友评论