Thread(下)【016】


-------
TestDeadLock------------------------------------------------------------------------

package com.testthread2;

public class TestDeadLock implements Runnable {
	public int flag  ;
	static Object o1 = new Object() ;
	static Object o2 = new Object() ;
	public void run() {
		System.out.println("flag" + flag) ;
		if(flag == 1) {
			synchronized(o1) {
				try {
					Thread.sleep(500) ;
				} catch (Exception e) {
					e.printStackTrace() ;
				}
				synchronized(o2) {
					System.out.println("1") ;
				}
			}
		}
		if(flag == 0) {
			synchronized(o2) {
				try {
					Thread.sleep(500) ;
				} catch(Exception e) {
					e.printStackTrace();
				}
				synchronized(o1) {
					System.out.println("0") ;
				}
			}			
		}
	}
	
	public static void main(String args[]) {
		TestDeadLock td1 = new TestDeadLock() ;
		TestDeadLock td2 = new TestDeadLock() ;
		td1.flag = 1 ;
		td2.flag = 0 ;
		Thread t1 = new Thread(td1) ;
		Thread t2 = new Thread(td2) ;
		t1.start();
		t2.start();
	}

}


-------
TestSynchronized------------------------------------------------------------------------

package com.testthread2;

//              
public class TestSync implements Runnable{
	Timer timer = new Timer() ;
	public static void main(String args[]) {
		TestSync test = new TestSync() ;
		Thread t1 = new Thread(test) ;
		Thread t2 = new Thread(test) ;
		t1.setName("t1") ;
		t2.setName("t2") ;
		t1.start();
		t2.start();
	}
	public void run() {
		timer.add(Thread.currentThread().getName()) ;
	}
}

class Timer {
	private static int num = 0;

	public synchronized void add(String name) { // synchronized      
		num++;
		try {
			Thread.sleep(1);
		} catch (InterruptedException e) {

		}
		System.out.println(name + ",,   " + num + "   timer   ");

	}
}


-------
Synchronized or NoSynchronized------------------------------------------------------------------------

package com.testthread2;

public class TT  implements Runnable {
	int b= 100 ;
	
	public synchronized void m1() throws Exception {
		b = 1000 ;
		Thread.sleep(5000) ;
		System.out.println("b="+b) ;
	}
	
	public synchronized void m2() throws Exception {
//	public void m2() throws Exception {
		Thread.sleep(2500) ;
		b = 2000 ;
	}
	
	public void run() {
		try {
			m1() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}
	
	public static void main(String args[]) throws Exception {
		TT tt = new TT() ;
		Thread t = new Thread(tt) ;
		t.start();
		
		tt.m2();
		System.out.println(tt.b) ;
	}

}

-------
Producer & Consumer ------------------------------------------------------------------------

package com.testthread2;

public class ProducerConsumer {
	public static void main(String args[]) {
		SyncStack ss = new SyncStack() ;
		Producer p = new Producer(ss) ;
		Consumer c = new Consumer(ss) ;
		new Thread(p).start() ;
		new Thread(c).start() ;		
	}

}

class WoTou {
	int id ;
	WoTou(int id) {
		this.id = id ;
	}
	public String toString() {
		return "WoTou :" + id ;
	}
}

class SyncStack {
	int index = 0 ; //      
	WoTou[] arrWT = new WoTou[6] ;
	
	public synchronized void push(WoTou wt) { //      
		while(index == arrWT.length) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}  //     wait:             wait
		}
		this.notify();
		arrWT[index] = wt ;
		index ++ ;
	}
	
	public synchronized WoTou pop() { //       
		while(index == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify() ;
		index -- ;
		return arrWT[index] ;
	}
}

class Producer implements Runnable {
	SyncStack ss = null ;
	public Producer(SyncStack ss) {
		this.ss =ss ;
	}
	
	public void run() {
		for(int i=0; i<25; i++) { //     
			WoTou wt = new WoTou(i) ;
			ss.push(wt) ;
			System.out.println("   :" + wt) ;
//			try {
//				Thread.sleep(1000) ;
//			} catch (InterruptedException e) {
//				e.printStackTrace() ;
//			}
		}
	}
}

class Consumer implements Runnable {
	SyncStack ss = null ;
	public Consumer(SyncStack ss) {
		this.ss =ss ;
	}
	
	public void run() {
		for(int i=0; i<20; i++) { //     
			WoTou wt = ss.pop();
			System.out.println("   :" +wt) ;
//			try {
//				Thread.sleep(1000) ;
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
		}
	}
}