生产者消费者
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-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?