JAVA死锁1

死锁的概念就是:当你去买东西的时候,店小二说给钱给货,你说给货给钱,你们僵住了,就死锁了

public class DeadLock {
	public static void main(String[] args) {
		Object o1 = new Object();
		Object o2 = new Object();
		
		Thread t1 = new T1(o1,o2);
		Thread t2 = new T2(o1,o2);
		
		t1.start();
		t2.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) {
				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,thread,object,String,Class)