多线程:生产者消费者模型
2022/4/20 23:15:36
本文主要是介绍多线程:生产者消费者模型,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、阻塞队列实现
public class Main { private static final int capacity=2, ptime=6, ctime=6; private static BlockingQueue<Integer> storage=new LinkedBlockingQueue<>(capacity); private static Integer count=0; static class Producer implements Runnable { @Override public void run() { for (int i = 0; i < ptime; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } try { //这里上锁是为了保证put和打印是原子的,如果不需要打印,可以不上锁。 synchronized (count) { storage.put(count); System.out.println("Producer:" + Thread.currentThread().getName() + " put: " + count++); } } catch (InterruptedException e) { e.printStackTrace(); } } } } static class Consumer implements Runnable { @Override public void run() { for (int i = 0; i < ctime; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } try { System.out.println("Consumer:" + Thread.currentThread().getName() + " take: " + storage.take()); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new Thread(new Consumer(),"C1").start(); new Thread(new Consumer(),"C2").start(); new Thread(new Producer(),"P1").start(); new Thread(new Producer(),"P2").start(); new Thread(new Producer(),"P3").start(); } }
2、Synchronized实现
class Storage { final int storageSize; private int countNow = 0; Storage(int storageSize) { this.storageSize = storageSize; } public void produce() { System.out.print("Producer " + Thread.currentThread().getName()); if (countNow < storageSize) { countNow++; System.out.print(" ##SUCCESS##, STORAGE:" + countNow); } else { System.out.print(" ##FAILTED:STORAGE FULL##"); } System.out.println(" Producer=========================="); } public void consume() { System.out.print("Consumer " + Thread.currentThread().getName()); if (countNow > 0) { countNow--; System.out.println(" ##SUCCESS##, STORAGE:" + countNow); } else { System.out.println(" ##FAILTED: STORAGE EMPTY##"); } } public int getCount(){ return countNow; } } public class Main { private static Storage storage; static class Producer implements Runnable { @Override public void run() { for (int i = 0; i < storage.storageSize; i++) { synchronized (storage) { storage.produce(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } } static class Consumer implements Runnable { @Override public void run() { for (int i = 0; i < storage.storageSize; i++) { synchronized (storage) { storage.consume(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { storage=new Storage(10); new Thread(new Producer(),"P1").start(); new Thread(new Producer(),"P2").start(); new Thread(new Producer(),"P3").start(); new Thread(new Consumer(),"C1").start(); new Thread(new Consumer(),"C2").start(); } }
生产者和消费者都对同一仓库对象上锁。
3、ReentrantLock实现
4、信号量实现
这篇关于多线程:生产者消费者模型的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略