java线程死锁一定会一直僵持下去吗?
如果是僵持了几十秒,然后就过去了,这个算不算死锁?下面的例子来自于网上,运行后也就僵持个十来秒,不知道算不算死锁,从逻辑上看应该是发生了相互等待。publicclassD...
如果是僵持了几十秒,然后就过去了,这个算不算死锁?
下面的例子来自于网上,运行后也就僵持个十来秒,不知道算不算死锁,从逻辑上看应该是发生了相互等待。
public class DeadLock implements Runnable {
public int flag = 1;
Object o1 = new Object();
Object o2 = new Object();
@Override
public void run() {
System.out.println("flag: " + flag);
// 当flag==1时,锁住o1
if (flag == 1) {
System.out.println("flag==1------>");
synchronized (o1) {
System.out.println("flag==1锁住o1------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 锁住o2
synchronized (o2) {
System.out.println("flag==1锁住o2------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
if (flag == 2) {
System.out.println("flag==2------>");
synchronized (o2) {
System.out.println("flag==2锁住o2------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized (o1) {
System.out.println("flag==2锁住o1------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class TestDeadLock {
public static void main(String[] args) {
DeadLock d1 = new DeadLock();
DeadLock d2 = new DeadLock();
DeadLock d3 = new DeadLock();
d1.flag = 1;
d2.flag = 2;
d2.flag = 1;
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
Thread t3 = new Thread(d3);
t1.start();
t2.start();
t3.start();
}
} 展开
下面的例子来自于网上,运行后也就僵持个十来秒,不知道算不算死锁,从逻辑上看应该是发生了相互等待。
public class DeadLock implements Runnable {
public int flag = 1;
Object o1 = new Object();
Object o2 = new Object();
@Override
public void run() {
System.out.println("flag: " + flag);
// 当flag==1时,锁住o1
if (flag == 1) {
System.out.println("flag==1------>");
synchronized (o1) {
System.out.println("flag==1锁住o1------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 锁住o2
synchronized (o2) {
System.out.println("flag==1锁住o2------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
if (flag == 2) {
System.out.println("flag==2------>");
synchronized (o2) {
System.out.println("flag==2锁住o2------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized (o1) {
System.out.println("flag==2锁住o1------>");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class TestDeadLock {
public static void main(String[] args) {
DeadLock d1 = new DeadLock();
DeadLock d2 = new DeadLock();
DeadLock d3 = new DeadLock();
d1.flag = 1;
d2.flag = 2;
d2.flag = 1;
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
Thread t3 = new Thread(d3);
t1.start();
t2.start();
t3.start();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询