Java多執行緒之併發協作生產者消費者設計模式

2021-09-11 10:58:03 字數 4658 閱讀 3328

/**

* created by yuandl on 2016-10-11./**

* 資源

*/public

class

resource

catch (interruptedexception e)

}number++;//生產乙個

system.out.println(thread.currentthread().getname() + "生產者------------" + number);

flag = true;//將資源標記為已經生產

notify();//喚醒在等待操作資源的執行緒(佇列)

}/**

* 消費資源

*/public

synchronized

void

destroy

() catch (interruptedexception e)

}system.out.println(thread.currentthread().getname() + "消費者****" + number);

flag = false;

notify();

}}複製**

/**

* created by yuandl on 2016-10-11.

* /**

* 生產者

*/public

class

producer

implements

runnable

@override

public

void

run()

catch (interruptedexception e)

resource.create();}}

}複製**

/**

* 消費者

*/public

class

consumer

implements

runnable

@override

public

void

run()

catch (interruptedexception e)

resource.destroy();}}

}複製**

/**

* created by yuandl on 2016-10-11.

*/public

class

producerconsumertest

}複製**

thread-0生產者------------1

thread-1消費者****1

thread-0生產者------------2

thread-1消費者****2

thread-0生產者------------3

thread-1消費者****3

thread-0生產者------------4

thread-1消費者****4

thread-0生產者------------5

thread-1消費者****5

thread-0生產者------------6

thread-1消費者****6

thread-0生產者------------7

thread-1消費者****7

thread-0生產者------------8

thread-1消費者****8

thread-0生產者------------9

thread-1消費者****9

thread-0生產者------------10

thread-1消費者****10複製**

以上列印結果可以看出沒有任何問題

/**

* created by yuandl on 2016-10-11.

*/public

class

producerconsumertest

}複製**

thread-0生產者------------100

thread-3消費者****100

thread-0生產者------------101

thread-3消費者****101

thread-2消費者****101

thread-1生產者------------102

thread-3消費者****102

thread-0生產者------------103

thread-2消費者****103

thread-1生產者------------104

thread-3消費者****104

thread-1生產者------------105

thread-0生產者------------106

thread-2消費者****106

thread-1生產者------------107

thread-3消費者****107

thread-0生產者------------108

thread-2消費者****108

thread-0生產者------------109

thread-2消費者****109

thread-1生產者------------110

thread-3消費者****110

複製**

原因分析

解決方案

/**

* created by yuandl on 2016-10-11./**

* 資源

*/public

class

resource

catch (interruptedexception e)

}number++;//生產乙個

system.out.println(thread.currentthread().getname() + "生產者------------" + number);

flag = true;//將資源標記為已經生產

notify();//喚醒在等待操作資源的執行緒(佇列)

}/**

* 消費資源

*/public

synchronized

void

destroy

() catch (interruptedexception e)

}system.out.println(thread.currentthread().getname() + "消費者****" + number);

flag = false;

notify();

}}複製**

執行結果

原因分析

解決方案

/**

* created by yuandl on 2016-10-11./**

* 資源

*/public

class

resource

catch (interruptedexception e)

}number++;//生產乙個

system.out.println(thread.currentthread().getname() + "生產者------------" + number);

flag = true;//將資源標記為已經生產

notifyall();//喚醒在等待操作資源的執行緒(佇列)

}/**

* 消費資源

*/public

synchronized

void

destroy

() catch (interruptedexception e)

}system.out.println(thread.currentthread().getname() + "消費者****" + number);

flag = false;

notifyall();

}}複製**

thread-0生產者------------412

thread-2消費者****412

thread-0生產者------------413

thread-3消費者****413

thread-1生產者------------414

thread-2消費者****414

thread-1生產者------------415

thread-2消費者****415

thread-0生產者------------416

thread-3消費者****416

thread-1生產者------------417

thread-3消費者****417

thread-0生產者------------418

thread-2消費者****418

thread-0生產者------------419

thread-3消費者****419

thread-1生產者------------420

thread-2消費者****420

複製**

以上就大功告成了,沒有任何問題

Java多執行緒之消費者生產者模式

這個例項應該是學習執行緒的乙個經典例子,生產者和消費者模式。寫的很好,詳細請看內容。author shijin 生產者與消費者模型中,要保證以下幾點 1 同一時間內只能有乙個生產者生產 生產方法加鎖sychronized 2 同一時間內只能有乙個消費者消費 消費方法加鎖sychronized 3 生...

java多執行緒之消費者生產者模式

author shijin 生產者與消費者模型中,要保證以下幾點 1 同一時間內只能有乙個生產者生產 生產方法加鎖sychronized 2 同一時間內只能有乙個消費者消費 消費方法加鎖sychronized 3 生產者生產的同時消費者不能消費 生產方法加鎖sychronized 4 消費者消費的同...

Java 多執行緒之生產者消費者模型

package com.yuanlief public class main 共享資料類 class mydata 共享資料控制類 class sharedata catch interruptedexception e this.data data writeable false 標記已經生產 n...