1. 程式人生 > >生產者/消費者程式碼(使用notify和wait實現)

生產者/消費者程式碼(使用notify和wait實現)

package cn.wzy.Lock;

import java.util.Random;

public class ConsumerAndProducer {

	static String[] food = new String[5];

	static String take() throws InterruptedException {
		synchronized (food) {
			while (true) {
				for (int i = 0; i < 5; i++) {
					if (food[i] != null) {
						String tmp = food[0];
						food[0] = null;
						food.notifyAll();
						System.out.println(Thread.currentThread().getId() + " 獲得了 " + i + " 的資料");
						return tmp;
					}
				}
				food.wait();
			}
		}
	}

	static void put(String data) throws InterruptedException {
		synchronized (food) {
			while (true) {
				for (int i = 0; i < 5; i++) {
					if (food[i] == null) {
						food[i] = data;
						food.notifyAll();
						System.out.println(Thread.currentThread().getId() + " 生產了 " + i + " 的資料");
						return;
					}
				}
				food.wait();
			}
		}
	}

	public static void main(String[] args) {
		Random random = new Random();
		for (int i = 0; i < 10; i++) {
			if (i % 2 == 1) {
				new Thread(()->{
					try {
						Thread.sleep(1000);
						take();
					} catch (InterruptedException e) {
						e.printStackTrace();
						Thread.interrupted();
					}
				}).start();
			} else {
				new Thread(()->{
					try {
						put(random.nextLong() + "");
					} catch (InterruptedException e) {
						e.printStackTrace();
						Thread.interrupted();
					}
				}).start();
			}
		}
	}
}