基於Java多線程機制的生產者-消費者模型類比

來源:互聯網
上載者:User
/*  *Author:Junyi Sun  *From:CCNU  *E-mail:fxsjy@yahoo.com.cn  */  import java.io.*; class Semaphore{    private int count;    private int cnt_max;    public Semaphore(int cnt,int cntm){         count=cnt;         cnt_max=cntm;    }    public synchronized void  p(){         while(count<=0){             try{              wait();             }             catch(InterruptedException e){              }        }         count--;         notify();    }    public synchronized void v(){         while(count>=cnt_max){             try{              wait();             }             catch(InterruptedException e){              }        }         count++;         notify();     }} class Producer extends Thread{    private Semaphore full,empty,mux;    private int total;    Producer(Semaphore _full,Semaphore _empty,Semaphore _mux,int _total){            full=_full;         empty=_empty;         mux=_mux;          total=_total;    }    private  void tell(int i){             System.out.print("生產了第");             System.out.print(i);             System.out.print("個/n");    }    public  void run(){        int i;         for(i=0;i<total;){             empty.p();                 mux.p();             tell(i);             mux.v();             i++;             full.v();         }    }} class Consumer extends Thread{    private Semaphore full,empty,mux;    private int total;    Consumer(Semaphore _full,Semaphore _empty,Semaphore _mux,int _total){         full=_full;         empty=_empty;         mux=_mux;         total=_total;    }    private  void tell(int i){             System.out.print("消費了第");             System.out.print(i);             System.out.print("個/n");    }    public  void run() {         int i;         for(i=0;i<total;){             full.p();             mux.p();             tell(i);             mux.v();             i++;             empty.v();         }    }} class IPC{    public static void main(String args[]){         Semaphore full,empty,mux;         int size=0,total=0;         System.out.println("請輸入倉庫的大小");         try{             size=Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());         }catch(IOException e){         };         System.out.println("請輸入要生產的總量");         try{              total=Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());          }catch(IOException e){        };          full=new Semaphore(0,size);         empty=new Semaphore(size,size);         mux=new Semaphore(1,1);         Producer producer=new Producer(full,empty,mux,total);         Consumer consumer=new Consumer(full,empty,mux,total);         producer.start();         while(!producer.isAlive());         consumer.start();         try{             System.in.read();         }         catch(IOException e){};    }} 

測試效果:

________________________________
請輸入倉庫的大小
1
請輸入要生產的總量
5
生產了第0個
消費了第0個
生產了第1個
消費了第1個
生產了第2個
消費了第2個
生產了第3個
消費了第3個
生產了第4個
消費了第4個

__________________________________

請輸入倉庫的大小
2
請輸入要生產的總量
5
生產了第0個
生產了第1個
消費了第0個
生產了第2個
消費了第1個
生產了第3個
消費了第2個
生產了第4個
消費了第3個
消費了第4個

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.