生产者消费者例子

使用wait和notify方法实现消费者生产者例子。

首先设计一个生产者类:

package com.freshbin.day2.two;

import java.util.LinkedList;
import java.util.List;

/**
 * 生产者类
 * 
 * @author freshbin
 * @date 2019年2月26日 下午3:31:57
 */
public class Producer implements Runnable {
	private List<Integer> myList;
	private Integer size;
	public static int count = 50;
	public static boolean isBreak = false;

	public Producer(List<Integer> myList, Integer size) {
		this.myList = myList;
		this.size = size;
	}
	
	@Override
	public void run() {
		try {
			while(true) {
				if(Consumer.isBreak) {
					System.out.println("消费者线程已中断!生产者也自动中断!");
					break;
				}
				
				if(Thread.currentThread().isInterrupted()) {
					System.out.println("生产者线程中断!");
					isBreak = true;
					break;
				}
				
				synchronized (myList) {
					if(myList.size() >= size) {
						System.out.println("已经满了!");
						myList.notifyAll();
						myList.wait();
					} else {
						count--;
						int number = (int)(Math.random()*10);
						myList.add(number);
						System.out.println("生产了一个:" + number);
						if(count <= 0) {
							System.out.println("生产者达到最大次数!");
							myList.notifyAll();
							Thread.currentThread().interrupt();
						}
						myList.notifyAll();
					}
				}
				
				if(!Thread.currentThread().isInterrupted()) {
					Thread.sleep(100);
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

消费者类:

package com.freshbin.day2.two;

import java.util.LinkedList;
import java.util.List;

/**
 * 消费者类
 * 
 * @author freshbin
 * @date 2019年2月26日 下午3:31:57
 */
public class Consumer implements Runnable {
	private List<Integer> myList;
	public static int count = 50;
	public static boolean isBreak = false;

	public Consumer(List<Integer> myList) {
		this.myList = myList;
	}
	
	@Override
	public void run() {
		try {
			while(true) {
				if(Producer.isBreak) {
					System.out.println("生产者线程已中断!消费者也自动中断!");
					break;
				}
				
				if(Thread.currentThread().isInterrupted()) {
					System.out.println("消费者线程中断!");
					isBreak = true;
					break;
				}
				
				synchronized (myList) {
					if(myList.size() > 0) {
						count--;
						int number = myList.get(myList.size() - 1);
						myList.remove(myList.size()-1);
						System.out.println("消费了一个:" + number);
						if(count <= 0) {
							System.out.println("消费者次数达到最大!");
							myList.notifyAll();
							Thread.currentThread().interrupt();
						}
						myList.notifyAll();
					} else {
						System.out.println("没有数量了!");
						myList.notifyAll();
						myList.wait();
					}
				}
				
				if(!Thread.currentThread().isInterrupted()) {
					Thread.sleep(100);
				}
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

客户端主类:

package com.freshbin.day2.two;

import java.util.LinkedList;
import java.util.List;

/**
 *  2.使用 wait notify 实现一个队列,队列有2个方法,add 和 get 。
 *  add方法往队列中添加元素,get方法往队列中获得元素。队列必须是线程安全的。
 *  如果get执行时,队列为空,线程必须阻塞等待,直到有队列有数据。
 *  如果add时,队列已经满,则add线程要等待,直到队列有空闲空间。
 *  实现这么一个队列,并写一个测试代码,使他工作在多线程的环境下,证明,它的工作是正确的。
 * 
 * @author freshbin
 * @date 2019年2月26日 下午3:05:41
 */
public class Two {
	public static void main(String[] args) throws InterruptedException {
		List<Integer> myList = new LinkedList<>();
		for(int i = 0; i < 5; i++) {
			myList.add(i);
		}
		
		Thread producer = new Thread(new Producer(myList, 5));
		producer.start();
		
		Thread consumer = new Thread(new Consumer(myList));
		consumer.start();
	}
}

控制台打印:

 

github地址:https://github.com/freshbin/ThreadDemo/tree/master/src/com/freshbin/day2/two

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值