天天看点

生产者消费者例子

使用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