java ----死锁

/* 死锁
 * 同步中嵌套同步。
 * 死锁的概念就是:当你去买东西的时候,店小二说给钱给货,你说给货给钱,你们僵住了,就死锁了
 * */
public class DeadLockDemo {
	public static void main(String[] args) {
		Object o1 = new Object();
		Object o2 = new Object();
		new T1(o1, o2).start();
		new T2(o1, o2).start();
	}
}
class T1 extends Thread {

	Object o1;
	Object o2;

	T1(Object o1, Object o2) {
		this.o1 = o1;
		this.o2 = o2;
	}
	public void run() {
		synchronized (o1) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			synchronized (o2) {
			}
		}
	}
}
class T2 extends Thread {
	Object o1;
	Object o2;
	T2(Object o1, Object o2) {
		this.o1 = o1;
		this.o2 = o2;
	}
	public void run() {
		synchronized (o2) {
			synchronized (o1) {

			}
		}
	}
}

你可能感兴趣的:(java多线程)