`

生产者消费者(二)

    博客分类:
  • JDK
 
阅读更多
需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓库可以存放10个产品。 第一批产品需要生产20个产品,并消费完毕。

其实使用wait/notify模式实现差不多,只是使用的时候要注意防止“死锁”。
blockingQueue的实现与notify的实现效率差不多.

package ycl.learn.effective.java.thread.pc;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class PublicResourceBlockingQueue {

	private BlockingQueue<Product> queue = new LinkedBlockingQueue<Product>(10);

	private AtomicInteger index = new AtomicInteger(1); 

	public static final int MAX_RESOURCE = 20;

	public boolean increace() {
		try {
			if (isComplete()) {// 生产任务完成
				return false;
			}
			int tindex = index.getAndIncrement();
			Product p = new Product(tindex);
			queue.put(p);
			System.out.println(Thread.currentThread() + "increace:" + tindex);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean decreace() {
		try {
			if (isComplete() && isEmpty()) {
				return false;
			} 
			synchronized(this){
				if(!isEmpty()){
					Product p = queue.take();
					System.out.println(Thread.currentThread() + "decreace:" + p.index);
				}
			} 
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 判断是否空了
	 */
	public boolean isEmpty() {
		return queue.isEmpty();
	}

	/**
	 * 判断生产完毕
	 */
	public boolean isComplete() {
		return index.get() > MAX_RESOURCE;
	}

	class Product {
		public int index;

		public Product(int index) {
			this.index = index;
		}
	}
}


public static void anotherone() throws InterruptedException{
		long start = System.currentTimeMillis();
		PublicResourceBlockingQueue ps = new PublicResourceBlockingQueue();
		CustomerB c = new CustomerB(ps);
		ProductorB p = new ProductorB(ps);
		Thread ct = new Thread(c,"customer0");
		Thread ct1 = new Thread(c,"customer1");
		Thread ct2 = new Thread(c,"customer2");
		Thread ct3 = new Thread(c,"customer3");
		Thread ct4 = new Thread(c,"customer4");
		Thread ct5 = new Thread(c,"customer5");
		Thread ct6 = new Thread(c,"customer6");
		
		Thread pt = new Thread(p,"productor0");
		Thread pt1 = new Thread(p,"productor1");
		Thread pt2 = new Thread(p,"productor2");
		Thread pt3 = new Thread(p,"productor3");
		Thread pt4 = new Thread(p,"productor4");
		Thread pt5 = new Thread(p,"productor5");
		Thread pt6 = new Thread(p,"productor6");
	  
		ct.start();
		ct1.start();
		ct2.start();
		ct3.start();
		ct4.start();
		ct5.start();
		ct6.start();
		
		pt.start();
		pt1.start();
		pt2.start();
		pt3.start();
		pt4.start();
		pt5.start();
		pt6.start();
		 
		
		ct.join();
		ct1.join();
		ct2.join();
		ct3.join();
		ct4.join();
		ct5.join();
		ct6.join();
		
		pt.join();
		pt1.join();
		pt2.join();
		pt3.join();
		pt4.join();
		pt5.join();
		pt6.join(); 
		System.out.println(System.currentTimeMillis()-start);//5531
	}


Thread[productor5,5,main]increace:5
Thread[productor2,5,main]increace:3
Thread[productor3,5,main]increace:4
Thread[productor6,5,main]increace:7
Thread[productor4,5,main]increace:6
Thread[productor1,5,main]increace:2
Thread[productor0,5,main]increace:1
Thread[productor1,5,main]increace:8
Thread[productor4,5,main]increace:9
Thread[customer1,5,main]decreace:6
Thread[productor0,5,main]increace:10
Thread[customer4,5,main]decreace:5
Thread[productor4,5,main]increace:11
Thread[productor6,5,main]increace:12
Thread[customer1,5,main]decreace:7
Thread[productor0,5,main]increace:13
Thread[customer2,5,main]decreace:4
Thread[productor3,5,main]increace:14
Thread[customer5,5,main]decreace:3
Thread[productor5,5,main]increace:15
Thread[customer4,5,main]decreace:2
Thread[productor0,5,main]increace:16
Thread[customer3,5,main]decreace:1
Thread[productor2,5,main]increace:17
Thread[customer6,5,main]decreace:8
Thread[customer0,5,main]decreace:9
Thread[customer3,5,main]decreace:10
Thread[customer5,5,main]decreace:11
Thread[productor1,5,main]increace:18
Thread[customer3,5,main]decreace:12
Thread[productor4,5,main]increace:19
Thread[productor5,5,main]increace:20
Thread[customer2,5,main]decreace:13
Thread[customer5,5,main]decreace:14
Thread[productor0,5,main] exist
Thread[customer1,5,main]decreace:15
Thread[productor5,5,main] exist
Thread[productor1,5,main] exist
Thread[customer4,5,main]decreace:16
Thread[customer2,5,main]decreace:17
Thread[productor3,5,main] exist
Thread[customer6,5,main]decreace:18
Thread[customer4,5,main]decreace:19
Thread[customer1,5,main]decreace:20
Thread[productor6,5,main] exist
Thread[customer5,5,main] exist
Thread[customer3,5,main] exist
Thread[productor2,5,main] exist
Thread[customer0,5,main] exist
Thread[productor4,5,main] exist
Thread[customer6,5,main] exist
Thread[customer2,5,main] exist
Thread[customer1,5,main] exist
Thread[customer4,5,main] exist
1234



实现的逻辑少了,这里需要注意的是,queue.take(),如果queue里再也不会有数据,将会”死锁“,防止多线程同时take最后一个,这里加了把锁。
分享到:
评论

相关推荐

    生产者消费者问题

    生产者消费者问题解决方案 生产者消费者问题是计算机科学中的一种经典问题,描述的是在多线程环境中,多个生产者线程和消费者线程之间的协作问题。生产者线程负责生产数据,并将其存储在缓冲区中,而消费者线程则从...

    多生产者与多消费者问题c++源码

    多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多生产者,多消费者问题源代码多...

    线程问题生产者消费者流程图

    生产者消费者流程图; 生产者消费者流程图。

    生产者消费者问题总结

    生产者消费者问题总结 信号量概念总结 经典生产者-消费者问题 较为复杂的生产者-消费者问题 华南理工大学生产者和消费者问题 个人总结

    Win丨linux丨操作系统实验二:生产者——消费者问题

    操作系统实验二:生产者——消费者问题 1. 在Windows操作系统上,利用Win32 API提供的信号量机制,编写应用程序实现生产者——消费者问题。 2. 在Linux操作系统上,利用Pthread API提供的信号量机制,编写应用程序...

    生产者消费者程序的实现

    生产者消费者的实现。可以自主地改变生产者,消费者的数目,和缓冲区。

    python实现生产者消费者并发模型

    多线程实现生产者消费者模型:锁(Lock)、信号量(Semaphore、BoundedSemaphore)、条件(Condition)、队列(Queue)、事件(Event) 多进程程实现生产者消费者模型:信号量(Semaphore)、条件(Condition)、...

    生产者与消费者实验报告

    生产者与消费者实验报告生产者与消费者实验报告生产者与消费者实验报告生产者与消费者实验报告

    生产者消费者实验报告.doc

    (2)信号量机制实现生产者和消费者对缓冲区的互斥访问。 (3)生产者生产产品时,要输出当前缓冲区冲产品的个数和存放产品的位置。 (4)消费者消费产品时,要输出当前缓冲区冲产品的个数和消费产品的位置。 (5)...

    pv操作解决生产者与消费者问题

    此外,pv操作还可以广泛应用于各种生产者消费者问题的解决中,使得系统更为可靠和高效。 pv操作是解决生产者与消费者问题的常用方法之一。通过pv操作,我们可以确保生产者进程和消费者进程之间的同步,避免数据的...

    操作系统课程设计——生产者消费者问题Java图形界面动态演示

    设计目的:通过研究Linux 的进程机制和信号量实现生产者消费者问题的并发控制。说明:有界缓冲区内设有20 个存储单元,放入/取出的数据项设定为1‐20 这20 个整型数。设计要求:1)每个生产者和消费者对有界缓冲区...

    生产者消费者问题 MFC 实现

    多线程同步互斥 生产者消费者问题 MFC 实现

    C语言实现生产者消费者问题

    C语言实现生产者消费者问题,分配具有n个缓冲区的缓冲池,作为共享资源。 定义两个资源型信号量empty 和full,empty信号量表示当前空的缓冲区数量,full表示当前满的缓冲区数量。 定义互斥信号量mutex,当某个进程...

    生产者 消费者 模式 c++

    生产者 消费者 模式 c++ 算是老外写的一个使用demo 可以参考一下

    生产者消费者问题(信号量)(linux)实现代码

    参考教材中的生产者消费者算法,创建5个进程,其中两个进程为生产者进程,3个进程为消费者进程。一个生产者进程试图不断地在一个缓冲中写入大写字母,另一个生产者进程试图不断地在缓冲中写入小写字母。3个消费者...

    LinuxC语言实现生产者和消费者模型

    LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC语言实现生产者和消费者模型LinuxC...

    生产者消费者图形化演示

    操作系统实验课实现的生产者消费者模型图形化演示。通过“企鹅吃苹果”这个小故事吧,苹果是生产者生产出来的,而企鹅是消费者。可以调节生产和消费的速度,也可以暂停程序方便演示。用C++实现的,使用的是纯粹的API...

Global site tag (gtag.js) - Google Analytics