多线程实现生产者消费者

对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的。就像学习每一门编程语言一样,Hello World!都是最经典的例子。
实际上,准确说应该是“生产者-消费者-仓储”模型,离开了仓储,生产者消费者模型就显得没有说服力了。
对于此模型,应该明确一下几点:
1、生产者仅仅在仓储未满时候生产,仓满则停止生产。
2、消费者仅仅在仓储有产品时候才能消费,仓空则等待。
3、当消费者发现仓储没产品可消费时候会通知生产者生产。

4、生产者在生产出可消费产品时候,应该通知等待的消费者去消费。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 主类 
 */
public class ProducerConsumer {
	
	public static void main(String[] args) {
		Storage storage = new Storage();
		ExecutorService service = Executors.newCachedThreadPool();
		Producer producerOne = new Producer("Producer One", storage);
		Producer producerTwo = new Producer("Producer Two", storage);
		Consumer consumerOne = new Consumer("Consumer One", storage);
		Consumer consumerTwo= new Consumer("Consumer Two", storage);
		Consumer consumerThree = new Consumer("Consumer Three", storage);
		service.submit(producerOne);
		service.submit(producerTwo);
		service.submit(consumerOne);
		service.submit(consumerTwo);
		service.submit(consumerThree);
	}
}
/**
 * 消费者
 */
public class Consumer implements Runnable {
	private String name;
	private Storage storage = null;
	public Consumer(String name, Storage storage){
		this.name = name;
		this.storage = storage;
	}
	public void run() {
		try{
			while(true) {
				System.out.println("ThreadId:  " + Thread.currentThread().getId() +  "   " + name + "  准备消费产品!");
				Product product = storage.pop();
				System.out.println("ThreadId:  " + Thread.currentThread().getId() +  "   " + name + "  已经消费 " + product.toString());
				Thread.sleep(2000);
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}
/**
 * 生产者
 */
public class Producer implements Runnable {
	private String name;
	private Storage storage = null;
	public Producer(String name, Storage storage) {
		this.name = name;
		this.storage = storage;
	}
	public void run() {
		while(true) {
			Product prodect = new Product((int)(Math.random()*10000));
			
			System.out.println("ThreadId:  " + Thread.currentThread().getId() +  "   "+ name + "准备生产 " + prodect.toString());
			try {
				storage.push(prodect);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("ThreadId:  " + Thread.currentThread().getId() +  "   " + name + "已经生产 " + prodect.toString());
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
/*
 * 产品类
 */
public class Product {
	private int id;
	public Product(int id) {
		this.id = id;
	}
	public String toString() {
		return "产品: " + this.id;
	}
}
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
 * 仓库类
 */
public class Storage {
	BlockingQueue<Product> queues = new LinkedBlockingQueue<Product>(10);
	public void push(Product p) throws InterruptedException {
		queues.put(p);
	}
	public Product pop() throws InterruptedException {
		return queues.take();
	}
}

转载链接:http://www.cnblogs.com/linjiqin/p/3217050.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值