LinkedBlockingQueue实现的生产者消费者

package com.jadyer.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * LinkedBlockingQueue实现的生产者消费者
 * @see --------------------------------------------------------------------------------------------------------------------
 * @see Java提供的线程安全的Queue可分为阻塞和非阻塞队列,其中非阻塞队列的典型是ConcurrentLinkedQueue,阻塞队列的是BlockingQueue
 * @see ConcurrentLinkedQueue是一个无界线程安全队列,它是按照FIFO(先进先出)原则对元素进行排序的,并且此队列不允许使用null元素
 * @see 关于接口BlockingQueue文档中是这么说的
 * @see A Queue that additionally supports operations that wait for the queue to become non-empty when
 * @see retrieving an element, and wait for space to become available in the queue when storing an element
 * @see 壹個支持额外操作的队列:当队列非空时才去获取壹個元素,当队列还有空间时才会存储壹個元素
 * @see --------------------------------------------------------------------------------------------------------------------
 * @see 以下BlockingQueue接口的几个常见标准实现
 * @see ArrayBlockingQueue-----有界的阻塞队列,它具有固定的尺寸,因此可在它被阻塞之前向其中放置有限数量的元素
 * @see                        其构造方法必须带一个int参数来指明BlockingQueue的大小,其所含对象是以FIFO(先入先出)顺序排序的
 * @see LinkedBlockingQueue----无界的阻塞队列,其所含的对象是以FIFO(先入先出)顺序排序的
 * @see                        若其构造方法带一个规定大小的参数则生成的BlockingQueue有大小限制,反之则由Integer.MAX_VALUE决定
 * @see PriorityBlockingQueue--类似于LinkedBlockQueue,但其所含对象的排序不是FIFO,而是依据对象自然排序或构造方法的Comparator决定
 * @see SynchronousQueue-------这是一个特殊的BlockingQueue,对其的操作必须是存放和获取交替完成
 * @see 阻塞队列可以解决非常大量的问题,而其方式与wait()和notifyAll()相比则简单并可靠得多
 * @see 由于LinkedBlockingQueue实现是线程安全的,实现了先进先出等特性,是作为生产者消费者的首选,它常用的方法是put()和take()
 * @see put()在队列满的时候会阻塞直到有队列成员被消费,take()在队列空的时候会阻塞直到有队列成员被放进来
 * @see 如果消费者试图从空队列中获取对象时,那么这些队列可以挂起消费者任务,并且当有更多元素可用时恢复消费者任务,生产者与之类似
 * @see --------------------------------------------------------------------------------------------------------------------
 * @create Nov 2, 2013 5:40:36 PM
 * @author 玄玉
 */
public class ProducerConsumer {
	public static void main(String[] args) {
		BlockingQueue<String> dataQueue = new LinkedBlockingQueue<String>();
		Producer p11 = new Producer("生产者01号", dataQueue);
		Producer p22 = new Producer("生产者02号", dataQueue);
		Consumer c11 = new Consumer("消费者01号", dataQueue);
		Consumer c22 = new Consumer("消费者02号", dataQueue);
		Consumer c33 = new Consumer("消费者03号", dataQueue);
		ExecutorService service = Executors.newCachedThreadPool();
		service.submit(p11);
		service.submit(p22);
		service.submit(c11);
		service.submit(c22);
		service.submit(c33);
	}
}


/**
 * 生产者
 */
class Producer implements Runnable {
	private String username;
	private BlockingQueue<String> dataQueue;
	public Producer(String username, BlockingQueue<String> dataQueue) {
		this.username = username;
		this.dataQueue = dataQueue;
	}
	public void run() {
		try {
			while (true) {
				int productID = (int)(Math.random()*1000); //产生0~999随机整数
				dataQueue.put(String.valueOf(productID));
				System.out.println(username + "已生产(" + productID + ")");
				Thread.sleep(500);
			}
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	}
}


/**
 * 消费者
 */
class Consumer implements Runnable {
	private String username;
	private BlockingQueue<String> dataQueue;
	public Consumer(String username, BlockingQueue<String> dataQueue) {
		this.username = username;
		this.dataQueue = dataQueue;
	}
	public void run() {
		try {
			while (true) {
				String product = dataQueue.take();
				System.out.println(username + "已消费(" + product + ")");
				Thread.sleep(500);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(生产者,消费者,线程池,synchronized,BlockingQueue)