5 12 生產者和消費者

2021-08-19 22:14:16 字數 952 閱讀 4707

一、生產者和消費者之間的關係

1、生產者將生產出來的資訊不斷存入乙個區域內,消費者將資訊從該區域內不斷讀取出來;

生產者錄入資訊

消費者讀取資訊

例:電影票

public class movie

public void setname(string name)

public string getinfo()

public void setinfo(string info)

public synchronized void set(string name , string info) catch (interruptedexception e)

}this.name=name;

try catch (interruptedexception e)

this.info=info;

flag=false;

super.notify();

}public synchronized void get() catch (interruptedexception e)

}system.out.println("名字:"+this.getname()+"--"+this.getinfo());

flag=true;

super.notify();

}public class productor implements runnable

@override

public void run() else}}

}public class consumer implements runnable

@override

public void run() catch (interruptedexception e)

this.movie.get();}}

}public class test

}

6 1 生產者 消費者問題

在多執行緒程式中,執行緒之間通常存在分工。在一種常見模式中,一些執行緒是生產者,一些是消費者。需要強制執行幾個同步約束才能使此系統正常工作 在緩衝區中新增或刪除專案時,緩衝區處於不一致狀態。因此,執行緒必須具有對緩衝區的獨佔訪問許可權。如果消費者執行緒在緩衝區為空時到達,則會阻塞,直到生產者新增新專...

26 生產者消費者模型

一 ipc 空間復用 中記憶體隔離開了多個程序直接不能直接互動 ipc指的就是程序間通訊 幾種方式 1 建立乙個共享檔案 缺點 效率較低 優點 理論上交換的資料量可以非常大 適用於 互動不頻繁,且資料量較大的情況 2 共享記憶體 缺點 資料量不大 優點 效率高 適用於 互動頻繁,但是資料量小 3 管...

c 11 生產者和消費者

include include include include using namespace std 定義兩個條件變數 佇列未滿 std condition variable queuenotfull 佇列不空 std condition variable queuenotempty 佇列鎖 st...