1. 一个特殊构造的程序
考虑下面这个专门为说明多线程中的死锁现象而构造的程序:
import java.util.LinkedList;
public class Stack {
public static void main(String[] args) {
final Stack stack = new Stack();
new Thread("push") {
@Override
public void run() {
for(int i = 0; i < 100; i++)
{
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
stack.push("object " + i);
}
}
}.start();
new Thread("pop") {
public void run() {
for(int i = 0; i < 100; i++)
{
try {
System.out.println(stack.pop());
} catch (Exception e) {}
}
}
}.start();
}