生产者与消费者

java之生产者与消费者

使用wait() 和 notify() 实现生产者和消费者模式

public class ThreadDemo17 {
	// 生产者和消费者是否继续操作,依据货架上商品的数据:queue.size()
	
	// 生产商品,存放在这个货架上
	//  某个整数        集合队列
	private Queue<Integer> queue = new LinkedList<Integer>();

	// 货架的大小,最多可容纳五个货物
	private static final int MAX_SIZE = 5; 
	
	public static void main(String[] args) {
		//先创建一外部类对象
		ThreadDemo17 outobj = new ThreadDemo17();
		
		//依据外部类对象,创建内部类对象
		Thread pthread = outobj.new Producer();
		Thread pthread2 = outobj.new Producer();
		Thread cthread = outobj.new Consumer();
		Thread cthread2 = outobj.new Consumer();
		Thread cthread3 = outobj.new Consumer();
		Thread cthread4 = outobj.new Consumer();
		Thread cthread5 = outobj.new Consumer();
		
		pthread.start();
		pthread2.start();
		cthread.start();
		cthread2.start();
		cthread3.start();
		cthread4.start();
		cthread5.start();
		
	}
	
	//创建成员内部类:生产者线程
	class Producer extends Thread {
		@Override
		public void run() {
			//生产者生产这个工作,不停地做!一直生产!货架满了,等会有空了,继续生产!
			while(true) {
				Thread.yield();
				
				synchronized (queue) {
					//判断货架是否满了
					// 如果货架满了,生产线程等待消费者消费,同时消费者唤醒生产者
					while(queue.size() == MAX_SIZE) {
						System.out.println("生产者 货架满了。");
						try {
							System.out.println("生产者 开始等待消费者消费");
							queue.wait();
							System.out.println("生产者 被唤醒,不再等待");
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					
					// 如果货架没满,有空的位置,生产一个商品,添加到货架上
					//    还需要唤醒:因货架空的时候,在等待的那些消费者
					Random rd = new Random();
					int product = rd.nextInt(10000);
					System.out.println("生产者 生产商品: " + product);
					
					queue.add(product);
					System.out.println("生产者 添加商品到货架,并唤醒消费者");
					queue.notifyAll();
				}
			}
		}
	}
	
	//创建成员内部类:消费者线程
	class Consumer extends Thread {
		@Override
		public void run() {
			
			//消费者是在不停地消费的
			while(true) {
				Thread.yield();
				
				synchronized (queue) {
					//判断货架上是否有商品
					//如果货架上没有商品,消费者等待生产者生产商品添加到货架
					while(queue.isEmpty()) {
						System.out.println("消费者 货架空了");
						try {
							System.out.println("消费者 开始等待");
							queue.wait();
							System.out.println("消费者 被唤醒 等待结束");
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					//如果货架上有商品,开始消费,从queue中取出一个商品
					//  唤醒通知生产者有人消费了,货架不再满了,去生产商品!
					Integer product = queue.remove();
					System.out.println("消费者 消费商品:" + product);
					System.out.println("消费者 唤醒因货架满而等待的生产线程");
					queue.notifyAll();
				}
			}
		}
	}
}

使用 BlockingQueue 实现生产者和消费者模式
直接使用BlockingQueue类中的put(e) 和 take()方法

public class ThreadDemo18 {
	// 生产者和消费者是否继续操作,依据货架上商品的数据:queue.size()
	
	// 生产商品,存放在这个货架上
	//  某个整数        集合队列
	//private Queue<Integer> queue = new LinkedList<Integer>();
	private BlockingQueue<Integer> queue = 
			new LinkedBlockingQueue<Integer>(MAX_SIZE);
	
	// 货架的大小,最多可容纳五个货物
	private static final int MAX_SIZE = 5; 
	
	public static void main(String[] args) {
		//先创建一外部类对象
		ThreadDemo18 outobj = new ThreadDemo18();
		
		//依据外部类对象,创建内部类对象
		Thread pthread = outobj.new Producer();
		Thread pthread2 = outobj.new Producer();
		
		Thread cthread = outobj.new Consumer();
		Thread cthread2 = outobj.new Consumer();
		Thread cthread3 = outobj.new Consumer();
		Thread cthread4 = outobj.new Consumer();
		Thread cthread5 = outobj.new Consumer();
		
		pthread.start();
		pthread2.start();
		
		cthread.start();
		cthread2.start();
		cthread3.start();
		cthread4.start();
		cthread5.start();
		
	}
	
	//创建成员内部类:生产者线程
	class Producer extends Thread {
		@Override
		public void run() {
			//生产者生产这个工作,不停地做!一直生产!货架满了,等会有空了,继续生产!
			while(true) {
				Thread.yield();
				if(queue.size() == MAX_SIZE) {
					System.out.println("生产者 货架满了。");
					System.out.println("生产者 开始等待消费者消费");
				}

				Random rd = new Random();
				int product = rd.nextInt(10000);
				System.out.println("生产者 生产商品: " + product);
				//queue.add(product);
				try {
					queue.put(product); 
						//如果队列没有空位置,put()方法会阻塞等待
						//  当有空位置时,不再阻塞,添加元素成功!
					System.out.println("生产者 添加商品到货架");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//创建成员内部类:消费者线程
	class Consumer extends Thread {
		@Override
		public void run() {
			
			//消费者是在不停地消费的
			while(true) {
				Thread.yield();
				
				if(queue.isEmpty()) {
					System.out.println("消费者 货架空了");
					System.out.println("消费者 开始等待");
				}
				
				//开始消费,从queue中取出一个商品
				//Integer product = queue.remove();
				try {
					Integer product = queue.take();
					System.out.println("消费者 消费商品:" + product);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值