死锁的问题
1.某个任务在等待另个任务,而后者有等待别的任务,这样一直下去,直到这个链条上的任务又在等待第一个任务释放锁,这种现象就称为死锁。
2.当以下四个条件同时满足时,就会发生死锁:
(1)互斥条件:线程使用的资源中至少有一个是不能共享的。这里,一根筷子一次就只能被一个哲学家使用。
(2)至少有一个进程持有一个资源,并且它在等待获取一个当前被别的进程持有的资源。也就是说,要发生死锁,哲学家必须拿着一根筷子并且等待另一根。
(3)资源不能被进程抢占。所有的进程必须把资源释放作为普通事件。哲学家很有礼貌,他们不会从其他哲学家那里抢筷子。
(4)必须有循环等待,这时,一个进程等待其它进程持有的资源,后者又在等待另一个进程持有的资源,这样一直下去,直到有一个进程在等待第一个进程持有的资源,使得大家都被锁住。
因为要发生死锁的话,所有这些条件必须全部满足,所以你要防止死锁的话,只需破坏其中一个即可。在程序中,防止死锁最容易的方法是破坏条件4。有这个条件的原因是每个哲学家都试图用特定的顺序拿筷子:先左后右。正因为如此,就可能会发生“每个人都拿着左边的筷子,并等待右边的筷子”的情况,这就是循环等待条件。然而,如果最后一个哲学家被初始化成先拿右边的筷子,后拿左边的筷子,那么这个哲学家将永远不会阻止其左边的哲学家拿起他/她右边的筷子,这就打破了循环等待。这只是问题的解决方法之一,你也可以通过破坏其它条件来防止死锁(具体细节请参考更高级的线程书籍)。Java对死锁并没有提供语言层面上的支持;能否通过小心的程序设计以避免死锁,取决于你自己。对于正在试图调试一个有死锁的程序的程序员来说,没有什么更好消息。
发生死锁的代码如下:
public class Chopstick {
private boolean taken = false;
public synchronized void take() throws InterruptedException {
while (taken)
wait();
taken = true;
}
public synchronized void drop() {
taken = false;
notifyAll();
}
}
public class Philosopher implements Runnable {
private Chopstick left;
private Chopstick right;
private final int id;
private final int ponderFactor;
private Random rand = new Random(47);
private void pause() throws InterruptedException {
if (ponderFactor == 0)
return;
TimeUnit.MILLISECONDS.sleep(rand.nextInt(ponderFactor * 250));
}
public Philosopher(Chopstick left, Chopstick right, int ident, int ponder) {
this.left = left;
this.right = right;
id = ident;
ponderFactor = ponder;
}
public void run() {
try {
while (!Thread.interrupted()) {
System.out.println(this + " " + "thinking");
pause();
// Philosopher becomes hungry
System.out.println(this + " " + "grabbing right");
right.take();
System.out.println(this + " " + "grabbing left");
left.take();
System.out.println(this + " " + "eating");
pause();
right.drop();
left.drop();
}
} catch (InterruptedException e) {
System.out.println(this + " " + "exiting via interrupt");
}
}
public String toString() {
return "Philosopher " + id;
}
}
public class DeadlockingDiningPhilosophers {
public static void main(String[] args) throws Exception {
int ponder = 0;
int size = 5;
ExecutorService exec = Executors.newCachedThreadPool();
Chopstick[] sticks = new Chopstick[size];
for (int i = 0; i < size; i++)
sticks[i] = new Chopstick();
for (int i = 0; i < size; i++)
exec.execute(new Philosopher(sticks[i], sticks[(i + 1) % size], i,
ponder));
TimeUnit.SECONDS.sleep(5);
System.out.println("Press 'Enter' to quit");
System.in.read();
exec.shutdownNow();
}
}
ponder 时间越小,发生死锁的概率越大
改进后的代码:
public class FixedDiningPhilosophers {
public static void main(String[] args) throws Exception {
int ponder = 0;
int size = 5;
ExecutorService exec = Executors.newCachedThreadPool();
Chopstick[] sticks = new Chopstick[size];
for (int i = 0; i < size; i++)
sticks[i] = new Chopstick();
for (int i = 0; i < size; i++)
if (i < (size - 1))
exec.execute(new Philosopher(sticks[i], sticks[i + 1], i,
ponder));
else
exec.execute(new Philosopher(sticks[0], sticks[i], i, ponder));
TimeUnit.SECONDS.sleep(5);
System.out.println("Press 'Enter' to quit");
System.in.read();
exec.shutdownNow();
}
}
主要是破坏循环等待的条件