java基础-生产者消费者
2021/8/12 1:06:19
本文主要是介绍java基础-生产者消费者,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
生产者消费者遵守规则:
例子:生产者-厨师 消费者-客人
厨师煎牛排,桌子上一块都没有 客人需要等待
桌子上牛排满了,厨师不需要生产 需要客人去拿(线程)
厨师在煎牛排过程中,如果没有煎完,客人不能去拿
客人没有拿完,不能把客人赶走
生产者消费者代码例子
定义桌子类存放牛排
public class Table { LinkedList<Object> list = new LinkedList<>(); private static final int Max=100; //存储产品 public synchronized void store(String product){ //如果仓库已满 生产者等待; while (list.size()>Max){ try { this.wait(); //wait();notify()这两个在同步代码块中,由锁对象调用 } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+"++存储了"+product);//把产品存进去 list.offer(product); } //取产品 public synchronized void take(){ //判断仓库是否为空,当仓库空时需要等待 while (list.size() <= 0){ try{ this.wait(); }catch (InterruptedException e){ e.printStackTrace(); } } Object product = list.poll();//把前面的产品取出来 System.out.println(Thread.currentThread().getName()+"--消费了"+product); //通知生产者继续生产 this.notify(); } }
定义生产者
public class Product extends Thread { private Table table; public Product(Table table) { super(); this.table = table; } @Override public void run(){ for(int i=0;i<30;i++){ String product = "产品编号:"+new Random().nextInt(100); table.store(product); } } }
定义消费者
public class ConsumerThread extends Thread { private Table table; public ConsumerThread(Table table) { super(); this.table = table; } @Override public void run(){ for(int i=0;i<30;i++){ table.take(); } } }
测试类:
class test{ public static void main(String[] args) { //创建桌子 Table table = new Table(); //创建3个生产者线程模拟三个人 Product p1= new Product(table); Product p2= new Product(table); Product p3= new Product(table); p1.setName("张三"); p2.setName("李四"); p3.setName("王五"); p1.start(); p2.start(); p3.start(); ConsumerThread c1 = new ConsumerThread(table); ConsumerThread c2 = new ConsumerThread(table); ConsumerThread c3 = new ConsumerThread(table); c1.setName("吃货1号"); c2.setName("吃货2号"); c3.setName("吃货3号"); c1.start(); c2.start(); c3.start(); } }
例子2:两个线程交替打印奇数和偶数
public class PrintNum { private int num=0; //打印奇数 public synchronized void printJi(){ //当num是偶数时需要等待 while (num % 2==0){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+":"+num); num++; //通知打印偶数 this.notify(); } //打印偶数 public synchronized void printOu(){ //当num是奇数时需要等待 while (num % 2==1){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+":"+num); num++; //通知打印奇数 this.notify(); } } class test09{ public static void main(String[] args) { PrintNum printNum = new PrintNum(); new Thread(new Runnable() { @Override public void run() { while (true){ printNum.printJi(); } } }).start(); new Thread(new Runnable() { @Override public void run() { while (true){ printNum.printOu(); } } }).start(); } }
这篇关于java基础-生产者消费者的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?