線程發生死鎖可能性很小
發生死鎖的原因一般是兩個對象的鎖相互等待造成的
在《Java線程
/**
* Java線程
*
* @author Administrator
*/
public class Test {
public static void main(String[] args) {
DeadlockRisk dead = new DeadlockRisk();
MyThread t
MyThread t
MyThread t
MyThread t
t
t
t
t
}
}
class MyThread extends Thread {
private DeadlockRisk dead;
private int a
MyThread(DeadlockRisk dead
this
this
this
}
@Override
public void run() {
dead
dead
}
}
class DeadlockRisk {
private static class Resource {
public int value;
}
private Resource resourceA = new Resource();
private Resource resourceB = new Resource();
public int read() {
synchronized (resourceA) {
System
synchronized (resourceB) {
System
return resourceB
}
}
}
public void write(int a
synchronized (resourceB) {
System
synchronized (resourceA) {
System
resourceA
resourceB
}
}
}
}
下面死鎖的情況發生了
本文出自
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26504.html