生產者與消費者模式是《設計模式》的書籍中推薦的23種模式之一,下麵我們根據示例實現此模式的代碼例子: 生產者A: //生產者A public class ProducerA extends Thread{ // 生產一個商品 private void produceGoods() { String ...
生產者與消費者模式是《設計模式》的書籍中推薦的23種模式之一,下麵我們根據示例實現此模式的代碼例子:
生產者A:
//生產者A public class ProducerA extends Thread{ // 生產一個商品 private void produceGoods() { String goodsName = ""+System.currentTimeMillis(); Goods good = new Goods(goodsName); synchronized (GoodsStore.getInstance().goodsList) {//同步 System.out.println("創建商品A:"+good.getGoodsName()); GoodsStore.getInstance().goodsList.add(good); } } @Override public void run() { while(true){//迴圈生成商品 long sleepTime = (long) (Math.random()*1000); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } this.produceGoods(); } } }
生產者B:
//生產者B public class ProducerB extends Thread{ // 生產一個商品 private void produceGoods() { String goodsName = "" + System.currentTimeMillis(); Goods good = new Goods(goodsName); synchronized (GoodsStore.getInstance().goodsList) {//同步 System.out.println("創建商品B:"+good.getGoodsName()); GoodsStore.getInstance().goodsList.add(good); } } @Override public void run() { while(true){//迴圈生成商品 long sleepTime = (long) (Math.random()*1000); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } this.produceGoods(); } } }
消費者A:
//消費者A public class ConsumerA extends Thread { // 消費一個商品 private void consumerGoods() { Goods good = GoodsStore.goodsList.getFirst(); synchronized (GoodsStore.goodsList) { System.out.println("消費商品A:" + good.getGoodsName()); GoodsStore.goodsList.remove(good); } } @Override public void run() { while (true) { long sleepTime = (long) (Math.random() * 1200); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("倉庫商品數量:" + GoodsStore.goodsList.size()); if (GoodsStore.goodsList.size() > 0) { this.consumerGoods(); } } } }
消費者B:
//消費者B public class ConsumerB extends Thread { // 消費一個商品 private void consumerGoods() { Goods good = GoodsStore.goodsList.getFirst(); synchronized (GoodsStore.goodsList) { System.out.println("消費商品B:" + good.getGoodsName()); GoodsStore.goodsList.remove(good); } } @Override public void run() { while (true) { long sleepTime = (long) (Math.random() * 1200); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("倉庫商品數量:" + GoodsStore.goodsList.size()); if (GoodsStore.goodsList.size() > 0) { this.consumerGoods(); } } } }
商品類:
//商品類 public class Goods { private String goodsName; public Goods(String goodsName){ this.goodsName = goodsName; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } }
商品倉庫:
import java.util.LinkedList; //商品倉庫 public class GoodsStore { //商品列表 public static LinkedList<Goods> goodsList; public static GoodsStore goodsStore; //創建單例對象 static{ goodsStore = new GoodsStore(); goodsList = new LinkedList();//構造一個空的對象 } // 單例對象 public static GoodsStore getInstance() { return goodsStore; } }
測試類:
//測試類 public class Test { public static void main(String[] args) { System.out.println("流水線生產開始......"); Thread producerA = new ProducerA(); producerA.start(); Thread producerB = new ProducerB(); producerB.start(); Thread consumerA = new ConsumerA(); consumerA.start(); Thread consumerB = new ConsumerB(); consumerB.start(); } }
運行結果如下:
流水線生產開始……
創建商品A:1503498407356
倉庫商品數量:1
消費商品A:1503498407356
創建商品A:1503498407430
創建商品B:1503498407492
倉庫商品數量:2
消費商品B:1503498407430
倉庫商品數量:1
消費商品A:1503498407492
創建商品A:1503498407856
倉庫商品數量:1
消費商品B:1503498407856
創建商品B:1503498408281
倉庫商品數量:1
消費商品A:1503498408281
創建商品A:1503498408527
倉庫商品數量:1
消費商品B:1503498408527
倉庫商品數量:0
倉庫商品數量:0
創建商品A:1503498408792
創建商品B:1503498409095
創建商品A:1503498409425
倉庫商品數量:3
消費商品B:1503498408792
創建商品B:1503498409686
倉庫商品數量:3
消費商品A:1503498409095
倉庫商品數量:2
消費商品B:1503498409425
創建商品B:1503498409992
倉庫商品數量:2
消費商品B:1503498409686
創建商品A:1503498410131
創建商品B:1503498410206
倉庫商品數量:3
消費商品A:1503498409992
倉庫商品數量:2
消費商品A:1503498410131