多執行緒 消費者與生產者案例

2021-08-04 07:23:52 字數 1126 閱讀 6648

生產者

package producer_consumer;

public class producer implements runnable

@override

public void run()

else

} }}

消費者

package producer_consumer;

public class consumer implements runnable

@override

public void run()

}}

資源管理區

package producer_consumer;

//共享的資源姓名與性別

public class shareresourse

//----生產開始------

this.name = name;

thread.sleep(10);

this.gender = gender;

//----生產結束------

this.notify(); //喚醒乙個消費者

isempty = false; //設定資源部位空

} catch (exception e)

} /**

* 消費者去除資料

*/synchronized public void popup()

//--------消費開始--------

thread.sleep(10);

system.out.println(name + " - " + gender);

//--------消費結束---------

this.notify(); //喚醒乙個生產者

isempty = true;

} catch (interruptedexception e)

}}

運作類:
package producer_consumer;

//測試**

public static void main(string args)

}

多執行緒 生產者消費者案例

案例需求 生產者消費者案例中包含的類 奶箱類 box 定義乙個成員變數,表示第x瓶奶,提供儲存牛奶和獲取牛奶的操作 生產者類 producer 實現runnable介面,重寫run 方法,呼叫儲存牛奶的操作 消費者類 customer 實現runnable介面,重寫run 方法,呼叫獲取牛奶的操作 ...

多執行緒 生產者消費者

這個就不多說了,直接上 include include using namespace std const unsigned short size of buffer 10 緩衝區長度 unsigned short productid 0 產品號 unsigned short consumeid 0...

生產者與消費者案例

手頭有兩個人 也就是兩個類 乙個做存操作,乙個做取操作,並且只有當存完或者取完方可進行令乙個操作。以此達到迴圈輸出的訪問操作。第一步 先寫測試生產者類與消費者類 和 執行緒操作msg類 生產者 public class product implements runnable override pub...