java 多线程实现生产者消费者模式

该文分别以栈和队列作为存储物品的仓库,生产物品以随机生产26个字母作为替代。以下是具体实现

(1)仓库接口

public interface Depository {
	public void producer(Character c);

	public Character consumer();
}
(2)以栈作为仓库

public class SynStack implements Depository {
	private static final int LENGTH = 6;
	private Stack<Character> data;

	public SynStack() {
		data = new Stack<Character>();
	}

	@Override
	public synchronized void producer(Character c) {
		if (data.size() == LENGTH) {
			try {
				System.out.println("仓库已满");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify();
		data.push(c);
		System.out.println("Producer:" + c);

	}

	@Override
	public synchronized Character consumer() {
		if (data.size() == 0) {
			try {
				System.out.println("仓库已空");
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		this.notify();
		Character result = data.pop();
		System.out.println("Consumser:" + result);
		return result;
	}

}

(3)以队列作为仓库

public class SynQueue implements Depository {
	private static final int LENGTH = 6;
	private LinkedList<Character> queue;

	public SynQueue() {
		queue = new LinkedList<Character>();
	}

	@Override
	public synchronized void producer(Character c) {
		if (queue.size() == LENGTH) {
			try {
				System.out.println("仓库已满");
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify();
		queue.add(c);
		System.out.println("Producer:" + c);
	}

	@Override
	public synchronized Character consumer() {
		if (queue.size() == 0) {
			try {
				System.out.println("仓库已空");
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.notify();
		Character result = queue.removeFirst();
		System.out.println("Consumer:" + result);
		return result;
	}

}

(4)生产者线程

public class Producer implements Runnable {
	private Depository depository;

	public Producer(Depository de) {
		depository = de;
	}

	@Override
	public void run() {
		for (int i = 0; i < 20; i++) {
			char c = (char) (Math.random() * 26 + 'A');
			depository.producer(c);
			try {
				Thread.sleep((long) (Math.random() * 300));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

(5)消费者线程

public class Consumer implements Runnable {
	private Depository depository;

	public Consumer(Depository de) {
		depository = de;
	}

	@Override
	public void run() {
		for (int i = 0; i < 20; i++) {
			depository.consumer();
			try {
				Thread.sleep((long) (Math.random() * 800));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

(6)测设类

public class ProducerConsumerTest {
	public static void main(String[] args) {
		// Depository depository = new SynStack();//测试以栈为仓库
		Depository depository = new SynQueue();// 测试以队列为仓库
		Runnable pRunnable = new Producer(depository);
		Runnable cRunnable = new Consumer(depository);

		Thread pThread = new Thread(pRunnable);
		Thread cThread = new Thread(cRunnable);

		pThread.start();
		cThread.start();
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值