生产者消费者
2022/7/11 23:23:38
本文主要是介绍生产者消费者,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
管程法【缓冲区法】
public class BufferPCTest_Shashlik { public static void main(String[] args) { SynContainer container = new SynContainer(); new Thread(new C(container)).start(); new Thread(new P(container)).start(); } /*消费者*/ static class C implements Runnable{ private SynContainer container; public C(SynContainer container){ this.container = container; } @Override public void run() { for (int i = 0; i < 200; i++) { try { System.out.println("吃羊肉串"+container.c().id); } catch (InterruptedException e) { e.printStackTrace(); } } } } /*生产者*/ static class P implements Runnable{ private SynContainer container; public P(SynContainer container){ this.container = container; } @Override public void run() { for (int i = 0; i < 200; i++) { try { container.p(new Shashlik(i)); System.out.println("烤羊肉串"+i); } catch (InterruptedException e) { e.printStackTrace(); } } } } /*盛放羊肉串的容器=>业务逻辑*/ static class SynContainer { int counter = 0; Shashlik[] container = new Shashlik[200]; /*烤羊肉串*/ public synchronized void p(Shashlik shashlik) throws InterruptedException { if(counter == container.length){ this.wait(); } counter++; container[counter] = shashlik; this.notifyAll(); } /*吃羊肉串*/ public synchronized Shashlik c() throws InterruptedException { if(counter == 0){ this.wait(); } Shashlik shashlik = container[counter]; counter--; this.notifyAll();; return shashlik; } } /*羊肉串*/ static class Shashlik{ int id; public Shashlik(int id){ this.id = id; } } }
标志位法
public static void main(String[] args) throws InterruptedException { Shashlik shashlik = new Shashlik(); new Thread(new P(shashlik)).start(); new Thread(new C(shashlik)).start(); } } /*生产者*/ class P implements Runnable { Shashlik shashlik; public P(Shashlik shashlik) { this.shashlik = shashlik; } @Override public void run() { for (int i = 1; i <= 200; i++) { this.shashlik.p(i); } } } /*消费者*/ class C implements Runnable { Shashlik shashlik; public C(Shashlik shashlik) { this.shashlik = shashlik; } @Override public void run() { for (int i = 1; i <= 200; i++) { this.shashlik.c(i); } } } /*羊肉串 业务逻辑*/ class Shashlik { boolean flag = true; public synchronized void p(int num) { try { if (!flag) { this.wait(); } /*烤羊肉串要等哦*/ TimeUnit.MILLISECONDS.sleep(1000); System.out.println("烤羊肉串" + num); this.notifyAll(); this.flag = !this.flag; } catch (InterruptedException e) { e.printStackTrace(); } } /*吃羊肉串*/ public synchronized void c(int num) { if (flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("吃羊肉串" + num); this.notifyAll(); this.flag = !this.flag; } }
这篇关于生产者消费者的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性