Java多线程中,有哪些经典案例可以体现生产者消费者模式?

更新于
2026-10-04 11:42:51
2阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计737个文字,预计阅读时间需要3分钟。

通过Java实现,5个盘子,生产者不断的产生热狗,最多5个。盘子上若有热狗,消费者就不断的吃热狗,直到吃完。

javapublic class ProducerAndConsumer { // 盘子数量 private static final int DISHES_COUNT=5; // 盘子数组 private static final Object[] dishes=new Object[DISHES_COUNT]; // 生产者标记 private static int producerIndex=0; // 消费者标记 private static int consumerIndex=0;

public static void main(String[] args) { // 创建生产者和消费者线程 Thread producer=new Thread(new Producer()); Thread consumer=new Thread(new Consumer()); // 启动线程 producer.start(); consumer.start(); }

// 生产者线程 static class Producer implements Runnable { @Override public void run() { while (true) { synchronized (dishes) { if (producerIndex

// 消费者线程 static class Consumer implements Runnable { @Override public void run() { while (true) { synchronized (dishes) { if (consumerIndex 通过java实现,5个盘子,生产者不停的生产热狗,最多5个。盘子上有热狗,消费者就不停的吃热狗,直到吃完

public class ProducerAndConsumer { /** *

title:main

*

function:

*@author mym *@param args */ static List hotDogs = new ArrayList (); //热狗集合 public static void main(String[] args) { for(int i = 1;i<4;i++){ new Thread(new Producer(i)).start(); } for(int j = 1;j<5;j++){ new Thread(new Consumer(j)).start(); } try { Thread.sleep(5000); System.exit(2); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static class Producer implements Runnable{ private int pid = -1; int i = 1; //标识热狗编号 public Producer(int pid) { this.pid = pid; } @Override public void run() { /** * 生产者生产热狗,只要有热狗生产出来就可以唤醒所有的等待线程, * 热狗数量超过了展示台5个,就等待消费 */ while(true){ try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//模拟生产消耗时间 synchronized(hotDogs){ if(hotDogs.size() < 5){ //添加热狗--生产 hotDogs.add(pid*100+i); System.out.println(pid+"号热狗师傅生产"+pid*100+i+"号热狗"); i++; //有了热狗就可以叫消费者来吃 hotDogs.notifyAll(); }else{ System.out.println("热狗拷满了,快来吃啊!"); try { hotDogs.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//等待 } } } } } static class Consumer implements Runnable{ private int cid = -1; public Consumer(int cid) { this.cid = cid; } @Override public void run() { /** * 消费者者生产热狗,只要消费了热狗就可一唤醒所有等待进程, * 热狗数量超过了展示台5个,就等待生产 */ while(true){ try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//模拟生产消耗时间 synchronized(hotDogs){ if(hotDogs.size() > 0){ //添加热狗--生产 System.out.println("消费者" + this.cid + "正在消费一个热狗,其编号为:" + hotDogs.remove(0)); //有了热狗就可以叫消费者来吃 hotDogs.notifyAll(); }else{ System.out.println("热狗mei了,快点做啊!"); try { hotDogs.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//等待 } } } } } }

本文共计737个文字,预计阅读时间需要3分钟。

通过Java实现,5个盘子,生产者不断的产生热狗,最多5个。盘子上若有热狗,消费者就不断的吃热狗,直到吃完。

javapublic class ProducerAndConsumer { // 盘子数量 private static final int DISHES_COUNT=5; // 盘子数组 private static final Object[] dishes=new Object[DISHES_COUNT]; // 生产者标记 private static int producerIndex=0; // 消费者标记 private static int consumerIndex=0;

public static void main(String[] args) { // 创建生产者和消费者线程 Thread producer=new Thread(new Producer()); Thread consumer=new Thread(new Consumer()); // 启动线程 producer.start(); consumer.start(); }

// 生产者线程 static class Producer implements Runnable { @Override public void run() { while (true) { synchronized (dishes) { if (producerIndex

// 消费者线程 static class Consumer implements Runnable { @Override public void run() { while (true) { synchronized (dishes) { if (consumerIndex 通过java实现,5个盘子,生产者不停的生产热狗,最多5个。盘子上有热狗,消费者就不停的吃热狗,直到吃完

public class ProducerAndConsumer { /** *

title:main

*

function:

*@author mym *@param args */ static List hotDogs = new ArrayList (); //热狗集合 public static void main(String[] args) { for(int i = 1;i<4;i++){ new Thread(new Producer(i)).start(); } for(int j = 1;j<5;j++){ new Thread(new Consumer(j)).start(); } try { Thread.sleep(5000); System.exit(2); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static class Producer implements Runnable{ private int pid = -1; int i = 1; //标识热狗编号 public Producer(int pid) { this.pid = pid; } @Override public void run() { /** * 生产者生产热狗,只要有热狗生产出来就可以唤醒所有的等待线程, * 热狗数量超过了展示台5个,就等待消费 */ while(true){ try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//模拟生产消耗时间 synchronized(hotDogs){ if(hotDogs.size() < 5){ //添加热狗--生产 hotDogs.add(pid*100+i); System.out.println(pid+"号热狗师傅生产"+pid*100+i+"号热狗"); i++; //有了热狗就可以叫消费者来吃 hotDogs.notifyAll(); }else{ System.out.println("热狗拷满了,快来吃啊!"); try { hotDogs.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//等待 } } } } } static class Consumer implements Runnable{ private int cid = -1; public Consumer(int cid) { this.cid = cid; } @Override public void run() { /** * 消费者者生产热狗,只要消费了热狗就可一唤醒所有等待进程, * 热狗数量超过了展示台5个,就等待生产 */ while(true){ try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//模拟生产消耗时间 synchronized(hotDogs){ if(hotDogs.size() > 0){ //添加热狗--生产 System.out.println("消费者" + this.cid + "正在消费一个热狗,其编号为:" + hotDogs.remove(0)); //有了热狗就可以叫消费者来吃 hotDogs.notifyAll(); }else{ System.out.println("热狗mei了,快点做啊!"); try { hotDogs.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }//等待 } } } } } }