ITeye博客

java线程:三种方式实现生产者消费者问题_1

rainyear · 2012-11-05 14:22 · 并行线程开发 · 非技术 · Markdown

java线程:三种方式实现生产者消费者问题_1

分类: java_线程2012-07-03 13:51 45人阅读 评论(0) 收藏 举报

本人采用java语言,用三种方式实现生产者——消费者的经典问题,至于生产者和消费的问题就不多说了,以下是第一种方式的具体代码:

[java] view plaincopy

  1. <span style="font-family:Arial;">
  2. /**
  3. * 消费者:生产者:生产者和消费者共用一个仓库
  4. */
  5. public class Consumer implements Runnable {
  6. private Storage storage;
  7. public Consumer(Storage storage) {
  8. this.storage = storage;
  9. }
  10. // 消费
  11. public void cunsume() {
  12. while (true) {
  13. storage.get();
  14. try {
  15. Thread.sleep(1500); // 每消费完一件产品,休息1.5秒
  16. } catch (InterruptedException e) {
  17. throw new RuntimeException(e);
  18. }
  19. }
  20. }
  21. @Override
  22. public void run() {
  23. this.cunsume();
  24. }
  25. }
  26. </span>

[java] view plaincopy

  1. <span style="font-family:Arial;">
  2. /**
  3. * 生产者:生产者和消费者共用一个仓库
  4. */
  5. public class Producer implements Runnable {
  6. private Storage storage;
  7. public Producer(Storage storage){
  8. this.storage = storage;
  9. }
  10. @Override
  11. public void run(){
  12. this.produce();
  13. }
  14. // 生产
  15. public void produce(){
  16. while(true){
  17. storage.put();
  18. try {
  19. Thread.sleep(1000); // 每生产完一件产品,休息1秒
  20. } catch (InterruptedException e) {
  21. throw new RuntimeException(e);
  22. }
  23. }
  24. }
  25. }
  26. </span>

[java] view plaincopy

  1. <span style="font-family:Arial;">
  2. /**
  3. * 仓库:
  4. * 先生产的先消费:使用队列的方式实现
  5. */
  6. public class Storage {
  7. public static final int MAX_SIZE = 100; // 仓库的最大货存
  8. // 容器,为了使用方便,没有面向接口编程,使用LinkedList作为实现类
  9. private LinkedList<Object> container = new LinkedList<Object>();
  10. // 从仓库中存放一个产品:
  11. public synchronized void put(){
  12. try {
  13. while (container.size() == MAX_SIZE) {
  14. try {
  15. this.wait();
  16. } catch (InterruptedException e) {
  17. throw new RuntimeException(e);
  18. }
  19. }
  20. System.out.println("put()等待结束,仓库现有数量为"+ container.size() +", 开始生产");
  21. container.addLast(new Object());
  22. } finally {
  23. this.notifyAll(); // 通知其他线程
  24. }
  25. }
  26. // 从仓库中取出一个产品:
  27. public synchronized Object get(){
  28. try {
  29. while (container.size() == 0) {
  30. try {
  31. this.wait();
  32. } catch (InterruptedException e) {
  33. throw new RuntimeException(e);
  34. }
  35. }
  36. System.out.println("get()等待结束,仓库现有数量为"+ container.size() +", 开始取出");
  37. return container.removeFirst();// 取出
  38. } finally {
  39. this.notifyAll(); // 通知其他线程
  40. }
  41. }
  42. }</span>

以下是测试代码:

[java] view plaincopy

  1. <span style="font-family:Arial;">
  2. public class Test {
  3. public static void main(String[] args) {
  4. Storage storage = new Storage();
  5. // 生产者和消费者两个线程类,用的必须是同一个仓库,即同一个仓库对象storage
  6. Consumer con = new Consumer(storage);
  7. Producer pro = new Producer(storage);
  8. new Thread(pro).start();
  9. new Thread(pro).start();
  10. new Thread(pro).start();
  11. new Thread(pro).start();
  12. //      new Thread(con).start();
  13. //      new Thread(con).start();
  14. //      new Thread(con).start();
  15. //      new Thread(con).start();
  16. new Thread(con).start();
  17. }
  18. }</span>

以下是测试代码结果:

get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产
get()等待结束,仓库现有数量为100, 开始取出
put()等待结束,仓库现有数量为99, 开始生产

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。