死锁没有成功,不能找出错误原因
public class TestDeadLock implements Runnable {
int frag ;
static Object o1 = new Object(),o2 = new Object();
public void run(){
System.out.println("frag"+frag);
if(frag==1){
synchronized(o1){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o2){
System.out.print("ok2");
}
}
if(frag==0){
synchronized(o2){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o1){
System.out.print("ok1");
}
}
}
public static void main(String[] args) {
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.frag=1;
td2.frag=0;
Thread th1 = new Thread(td1);
Thread th2 = new Thread(td2);
th1.start();
th2.start();
}}
public class TestDeadLock implements Runnable {
int frag ;
static Object o1 = new Object(),o2 = new Object();
public void run(){
System.out.println("frag"+frag);
if(frag==1){
synchronized(o1){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o2){
System.out.print("ok2");
}
}
if(frag==0){
synchronized(o2){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o1){
System.out.print("ok1");
}
}
}
public static void main(String[] args) {
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.frag=1;
td2.frag=0;
Thread th1 = new Thread(td1);
Thread th2 = new Thread(td2);
th1.start();
th2.start();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货