<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    keep moving!

    We must not cease from exploration. And the end of all our exploring will be to arrive where we began and to know the place for the first time.
    隨筆 - 37, 文章 - 2, 評論 - 3, 引用 - 0
    數(shù)據(jù)加載中……

    Design Pattern: Producer Consumer 模式

    Producer Consumer模式與 Guarded Suspension 模式 是類似的,只不過Guarded Suspension模式並不限制緩衝區(qū)的長度,Producer Consumer模式假設所生產的產品放置在一個長度有限制的緩衝區(qū)(就像是一個產品桌,它可以擺放的空間是有限的),如果緩衝區(qū)滿了,則生產者必須停止繼續(xù)將產品放到緩衝區(qū)中,直到消費者取走了產品而有了空間,而如果緩衝區(qū)中沒有產品,當然消費者必須等待,直到有新的產品放到緩衝區(qū)中。

    一個簡單的 UML 順序圖如下所示:
    ProducerConsumer
    簡單來說,Producer Consumer模式就像是加上了雙重防護與等待的Guarded Suspension模式,而它的兩個防護與等待的條件洽好相反,一個用Java實現(xiàn)的簡單流程架構如下:
    • ProductTable.java
    import java.util.LinkedList;

    public class ProductTable {
    private LinkedList products = new LinkedList();

    public synchronized void addProduct(Product product) {
    while(products.size() >= 2) { // 容量限制為 2
    try {
    wait();
    }
    catch(InterruptedException e) {}
    }

    products.addLast(product);
    notifyAll();
    }

    public synchronized Product getProduct() {
    while(products.size() <= 0) {
    try {
    wait();
    }
    catch(InterruptedException e) {}
    }

    Product product = (Product) products.removeFirst();
    notifyAll();

    return product;
    }
    }

    以下舉一個最簡單的:生產者每次生產一個整數(shù)並放置在桌子上,而消費者消耗整數(shù),桌子上一次只能放置一個整數(shù),如果桌子上已有整數(shù),則生產者等待消費者將整數(shù)消耗並通知生產者生產下一個整數(shù),如果桌子上沒有整數(shù),則消費者等待生產者生產整數(shù)並通知消費者可以消耗整數(shù)。
    • Producer.java
    public class Producer extends Thread {
    private ProductTable productTable;

    public Producer(ProductTable productTable) {
    this.productTable = productTable;
    }

    public void run() {
    System.out.println("Produce integer......");
    for(int product = 1; product <= 10; product++) {
    try {
    // wait for a random time
    Thread.sleep((int) Math.random() * 3000);
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    productTable.setIntProduct(product);
    }
    }
    }

    • Consumer.java
    public class Consumer extends Thread {
    private ProductTable productTable;

    public Consumer(ProductTable productTable) {
    this.productTable = productTable;
    }

    public void run() {
    for(int i = 1; i <= 10; i++) {
    try {
    // wait for a random time
    Thread.sleep((int) (Math.random() * 3000));
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    productTable.getProductInt();
    }
    }
    }

     

    生產者將產品放至桌上,而消費者將產品從桌上取走,所以桌子是個維護是否讓被放置或消耗產品的地方,由它來決定誰必須等待與通知:
    • ProductTable.java
    public class ProductTable {
    private int productInt = -1; // -1 for no product

    public synchronized void setIntProduct(int product) {
    if(productInt != -1) {
    try {
    wait();
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    }

    productInt = product;
    System.out.println("set (" + product + ")");
    notify();
    }

    public synchronized int getProductInt() {
    if(productInt == -1) {
    try {
    wait();
    }
    catch(InterruptedException e) {
    e.printStackTrace();
    }
    }

    int p = productInt;
    System.out.println("Get (" + productInt + ")");
    productInt = -1;

    notify();

    return p;
    }
    }

    生產者會生產10個整數(shù),而消費者會消耗10個整數(shù),由於桌上只能放置一個整數(shù),所以每生產一個就消耗一個。


    張金鵬 2007-04-17 10:56 發(fā)表評論

    文章來源:http://www.tkk7.com/jesson2005/articles/111195.html

    posted on 2008-09-07 11:06 大石頭 閱讀(188) 評論(0)  編輯  收藏 所屬分類: 多線程

    主站蜘蛛池模板: 999任你躁在线精品免费不卡| 亚洲日韩精品无码专区加勒比| 一级黄色毛片免费看| 免费看的黄色大片| 激情小说亚洲色图| 国产一区二区三区在线免费| 婷婷国产偷v国产偷v亚洲| 免费一级做a爰片久久毛片潮喷| 老司机福利在线免费观看| 夜色阁亚洲一区二区三区| 一级视频免费观看| 中文字幕精品亚洲无线码一区| baoyu116.永久免费视频| 亚洲AV日韩AV永久无码久久| 97青青草原国产免费观看| 亚洲国产精品久久人人爱| 成人男女网18免费视频| 香港经典a毛片免费观看看| 中文字幕专区在线亚洲| 99蜜桃在线观看免费视频网站| 亚洲一区二区三区不卡在线播放| 国内自产拍自a免费毛片| 一级毛片在线完整免费观看| 亚洲欧洲成人精品香蕉网| 成人免费黄色网址| 亚洲GV天堂无码男同在线观看| 亚洲麻豆精品国偷自产在线91| 国产激情免费视频在线观看| 亚洲天堂福利视频| 四虎1515hm免费国产| 久操免费在线观看| 亚洲精品无码人妻无码| 国产AV无码专区亚洲AV漫画| 亚洲精品视频在线免费| 免费观看又污又黄在线观看| 亚洲AV无码成人精品区在线观看 | 免费国产成人午夜私人影视 | 亚洲成人免费在线观看| 免费在线观看亚洲| 中文字幕亚洲综合精品一区| 日韩视频在线免费观看|