/* 資源類 */
class ShareValue {private int total;//判斷對象是否為空private boolean isEmpty=true;//判斷對象是否已滿private boolean isFull=true;public ShareValue(int total) {this.total = total;if(total>0) isEmpty=false;if(total<1000) isFull=false;}/** synchronized 方法控制對類成員變量的訪問:每個類實例對應一把鎖,每個 synchronized 方法都必須獲得調用該方法的類實例的鎖方能執行,* 否則所屬線程阻塞,方法一旦執行,就獨占該鎖,直到從該方法返回時才將鎖釋放,此后被阻塞的線程方能獲得該鎖,重新進入可執行狀態。* 這種機制確保了同一時刻對于每一個類實例,其所有聲明為 synchronized 的成員函數中至多只有一個處于可執行狀態* (因為至多只有一個能夠獲得該類實例對應的鎖),從而有效避免了類成員變量的訪問沖突(只要所有可能訪問類成員變量的方法均被聲明為 synchronized)。* */synchronized void putValue(int value){//取得當前線程名String name=Thread.currentThread().getName();while(isFull){display("Full ! ["+name+"] waites.");try {this.wait();} catch (InterruptedException e) {System.out.println(e);}}total+=value;if(total>1000){isFull=true;}display(name+",put: "+value+",old value: "+(total-value)+",now value"+total);isEmpty=false;//通知所有的等待線程notifyAll();}synchronized int getValue(int value){String name=Thread.currentThread().getName();while(isEmpty || total<value){display("Empty or not enough! ["+name+"] waits."); try {//進入等待狀態this.wait();} catch (InterruptedException e) {System.out.println(e);}}display(name+" get: "+value+",old value:"+total+",now value :"+(total-value));if(total-value>=0){total-=value;}if(total==0){isEmpty=true;}if(total<1000){isFull=false;}//通知所有等待的線程notifyAll();return value;}int getNowTotal(){return total;}private void display(String string) {System.out.println(string);}
}/* 生產者類 */
class Producer extends Thread {// 共享的ShareValue對象ShareValue share;// 要增加的值int value;public Producer(String name, ShareValue share, int value) {super(name);this.share = share;this.value = value;}public void run() {//同步share對象 ,直到當前代碼塊運行完畢后,share的對象鎖才會釋放synchronized (share) {try {sleep(100);} catch (InterruptedException e) {System.out.println(e);}share.putValue(value);}}
}/*消費者類*/
class Consumer extends Thread{//共享的ShareValue對象ShareValue share;//要減少的值int value;public Consumer(String name,ShareValue share, int value) {super(name);this.share = share;this.value = value;}public void run(){ //同步share對象,直到當前代碼運行完畢后,share的對象鎖才會釋放synchronized (share) {try {sleep(100);} catch (InterruptedException e) {System.out.println(e);}share.getValue(value);}}
} /* 測試主類 */
public class TestDemo {public static void main(String[] args) {ShareValue share=new ShareValue(0);Producer producer1=new Producer("producer1", share, 100);Consumer consumer=new Consumer("consumer", share, 300);Producer producer2=new Producer("producer2",share,950);Producer producer3=new Producer("producer3",share,50);
// Producer producer4=new Producer("producer4",share,50);
// Producer producer5=new Producer("producer5",share,50);producer1.start();consumer.start();producer2.start();producer3.start();
// producer4.start();
// producer5.start();}}