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

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

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

    細心!用心!耐心!

    吾非文人,乃市井一俗人也,讀百卷書,跨江河千里,故申城一游; 一兩滴辛酸,三四年學業,五六點粗墨,七八筆買賣,九十道人情。

    BlogJava 聯系 聚合 管理
      1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
    Producer Consumer模式與 Guarded Suspension 模式 是類似的,只不過Guarded Suspension模式並不限制緩衝區的長度,Producer Consumer模式假設所生產的產品放置在一個長度有限制的緩衝區(就像是一個產品桌,它可以擺放的空間是有限的),如果緩衝區滿了,則生產者必須停止繼續將產品放到緩衝區中,直到消費者取走了產品而有了空間,而如果緩衝區中沒有產品,當然消費者必須等待,直到有新的產品放到緩衝區中。

    一個簡單的 UML 順序圖如下所示:
    ProducerConsumer
    簡單來說,Producer Consumer模式就像是加上了雙重防護與等待的Guarded Suspension模式,而它的兩個防護與等待的條件洽好相反,一個用Java實現的簡單流程架構如下:
    • 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;
    }
    }

    以下舉一個最簡單的:生產者每次生產一個整數並放置在桌子上,而消費者消耗整數,桌子上一次只能放置一個整數,如果桌子上已有整數,則生產者等待消費者將整數消耗並通知生產者生產下一個整數,如果桌子上沒有整數,則消費者等待生產者生產整數並通知消費者可以消耗整數。
    • 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個整數,而消費者會消耗10個整數,由於桌上只能放置一個整數,所以每生產一個就消耗一個。
    posted on 2007-04-17 10:56 張金鵬 閱讀(509) 評論(0)  編輯  收藏 所屬分類: 多執行緒模式
    主站蜘蛛池模板: 亚洲色欲色欲综合网站| 亚洲电影一区二区| 亚洲成片观看四虎永久| 久久久久亚洲精品无码网址 | MM131亚洲国产美女久久| 久久91亚洲人成电影网站| 亚洲成aⅴ人片在线观| 国产99久久亚洲综合精品| 久久精品免费网站网| 免费观看激色视频网站bd| 国产成人aaa在线视频免费观看| 亚洲人成人77777网站| ASS亚洲熟妇毛茸茸PICS| 国产免费高清69式视频在线观看| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 在线涩涩免费观看国产精品 | 99久久免费精品视频| 香蕉视频在线观看免费国产婷婷| 亚洲性猛交XXXX| 久艹视频在线免费观看| 日本人护士免费xxxx视频| 亚洲精品视频在线播放| a级片免费在线观看| 亚洲国产电影av在线网址| 国产免费一级高清淫曰本片| 伊人久久大香线蕉亚洲| 牛牛在线精品观看免费正| 久九九精品免费视频| 亚洲成色在线综合网站| 国产精品久久久久久久久免费| 亚洲第一AV网站| 黄页免费的网站勿入免费直接进入| 亚洲av无码一区二区三区四区 | 男人都懂www深夜免费网站| 亚洲三级视频在线| ww在线观视频免费观看| 日韩一级片免费观看| 亚洲国产综合精品中文字幕| 国产精品成人亚洲| 午夜影视日本亚洲欧洲精品一区| 三年片在线观看免费大全|