乀閉丄眼﹋

有些人工作了20年,实际经验只有5年,有些人工作了5年,实际经验却又20年,你懂了没?

导航

重温wait()和notify()

Posted on 2012-08-17 10:47  乀閉丄眼﹋  阅读(234)  评论(0)    收藏  举报

    温故知新,接下来看看线程池。    

     同时启动3个线程A、B、C,A线程输出A 20次,B线程输出B 20次,C线程输出C 20次,要求输出结果为顺序输出A、B、C、A、B、C。。。

Java代码 
  1. class Agent{  
  2.     publicstaticint state = 1;  
  3.  
  4.     publicsynchronizedvoid sayA() {  
  5.         if(state != 1){  
  6.             try {  
  7.                 super.wait();  
  8.             } catch (Exception e) {  
  9.                 // TODO: handle exception  
  10.             }  
  11.         }  
  12.         System.out.println("A");  
  13.         state = 2;  
  14.         super.notify();  
  15.     }  
  16.     publicsynchronizedvoid sayB() {  
  17.         if(state != 2){  
  18.             try {  
  19.                 super.wait();  
  20.             } catch (Exception e) {  
  21.                 // TODO: handle exception  
  22.             }  
  23.         }  
  24.         System.out.println("B");  
  25.         state = 3;  
  26.         super.notify();  
  27.     }  
  28.     publicsynchronizedvoid sayC() {  
  29.         if(state != 3){  
  30.             try {  
  31.                 super.wait();  
  32.             } catch (Exception e) {  
  33.                 // TODO: handle exception  
  34.             }  
  35.         }  
  36.         System.out.println("C");  
  37.         state = 1;  
  38.         super.notify();  
  39.     }  
  40. }  
  41.  
  42. class A extends Thread{  
  43.     Agent agent;  
  44.     public A(Agent agent){  
  45.         this.agent = agent;  
  46.     }  
  47.     publicvoid run(){  
  48.         for(int i=0;i<20;i++){  
  49.             try {  
  50.                 Thread.sleep(200);  
  51.             } catch (Exception e) {  
  52.             }  
  53.             agent.sayA();  
  54.         }  
  55.     }  
  56. }  
  57. class B extends Thread{  
  58.     Agent agent;  
  59.     public B(Agent agent){  
  60.         this.agent = agent;  
  61.     }  
  62.     publicvoid run(){  
  63.         for(int i=0;i<20;i++){  
  64.             try {  
  65.                 Thread.sleep(200);  
  66.             } catch (Exception e) {  
  67.             }  
  68.             agent.sayB();  
  69.         }  
  70.     }  
  71. }  
  72. class C extends Thread{  
  73.     Agent agent;  
  74.     public C(Agent agent){  
  75.         this.agent = agent;  
  76.     }  
  77.     publicvoid run(){  
  78.         for(int i=0;i<20;i++){  
  79.             try {  
  80.                 Thread.sleep(200);  
  81.             } catch (Exception e) {  
  82.             }  
  83.             agent.sayC();  
  84.         }  
  85.     }  
  86. }  
  87.  
  88. publicclass TestWait {  
  89.     publicstaticvoid main(String[] args) {  
  90.         Agent agent = new Agent();  
  91.         new A(agent).start();  
  92.         new B(agent).start();  
  93.         new C(agent).start();  
  94.     }  
class Agent{
	public static int state = 1;

	public synchronized void sayA() {
		if(state != 1){
			try {
				super.wait();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		System.out.println("A");
		state = 2;
		super.notify();
	}
	public synchronized void sayB() {
		if(state != 2){
			try {
				super.wait();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		System.out.println("B");
		state = 3;
		super.notify();
	}
	public synchronized void sayC() {
		if(state != 3){
			try {
				super.wait();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		System.out.println("C");
		state = 1;
		super.notify();
	}
}

class A extends Thread{
	Agent agent;
	public A(Agent agent){
		this.agent = agent;
	}
	public void run(){
		for(int i=0;i<20;i++){
			try {
				Thread.sleep(200);
			} catch (Exception e) {
			}
			agent.sayA();
		}
	}
}
class B extends Thread{
	Agent agent;
	public B(Agent agent){
		this.agent = agent;
	}
	public void run(){
		for(int i=0;i<20;i++){
			try {
				Thread.sleep(200);
			} catch (Exception e) {
			}
			agent.sayB();
		}
	}
}
class C extends Thread{
	Agent agent;
	public C(Agent agent){
		this.agent = agent;
	}
	public void run(){
		for(int i=0;i<20;i++){
			try {
				Thread.sleep(200);
			} catch (Exception e) {
			}
			agent.sayC();
		}
	}
}

public class TestWait {
	public static void main(String[] args) {
		Agent agent = new Agent();
		new A(agent).start();
		new B(agent).start();
		new C(agent).start();
	}
}