Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257112
  • 博文数量: 49
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-13 00:59
个人简介

make it run,make it better,make it fast. https://github.com/liulanghaitun

文章分类

全部博文(49)

文章存档

2023年(1)

2022年(2)

2020年(4)

2019年(4)

2017年(15)

2016年(3)

2014年(3)

2013年(14)

分类: Windows平台

2013-06-04 18:35:10

生产者-消费者模型是一个并发协作模型。
生产者在共享数据区写数据,消费者从共享数据区读取数据。
如果共享区没有数据,消费者等待生产者存放数据。如果共享区有数据,消费者获取数据并且等待生产者存放数据。
 

点击(此处)折叠或打开

  1. /**
  2.  * 生产者
  3.  *
  4.  * @author renwei
  5.  *
  6.  */
  7. public class Producer extends Thread {

  8.     // 共享数据区
  9.     private ShareArea shareArea;

  10.     // 产生随机数
  11.     private final Random random = new Random();

  12.     public Producer(ShareArea share) {
  13.         shareArea = share;
  14.     }

  15.     @Override
  16.     public void run() {
  17.         while (true) {
  18.             shareArea.setShareValue(produceData());
  19.             sleepTime(500);
  20.         }
  21.     }

  22.     private void sleepTime(long time) {
  23.         try {
  24.             Thread.sleep(time);
  25.         } catch (InterruptedException e) {
  26.             System.out.println(e.toString());
  27.         }
  28.     }

  29.     //负责产生数据
  30.     private int produceData() {
  31.         final int value = random.nextInt(500);
  32.         System.out.println("Producer put a value: " + value);
  33.         return value;
  34.     }
  35. }


点击(此处)折叠或打开

  1. /**
  2.  * 消费者
  3.  *
  4.  * @author renwei
  5.  *
  6.  */
  7. public class Consumer extends Thread {

  8.     // 数据共享区
  9.     private ShareArea shareArea;

  10.     public Consumer(ShareArea share) {
  11.         shareArea = share;
  12.     }

  13.     @Override
  14.     public void run() {
  15.         while (true) {
  16.             final int result = shareArea.getShareValue();
  17.             consumerData(result);
  18.             sleepTime(500);
  19.         }
  20.     }

  21.     private void sleepTime(long time) {
  22.         try {
  23.             Thread.sleep(time);
  24.         } catch (InterruptedException e) {
  25.             System.out.println(e.toString());
  26.         }
  27.     }

  28.     // 处理数据
  29.     private void consumerData(int value) {
  30.         System.out.println("Consumer get a value: " + value + "n");
  31.     }
  32. }

点击(此处)折叠或打开

  1. /**
  2.  * 共享数据区
  3.  *
  4.  * @author renwei
  5.  *
  6.  */
  7. public class ShareArea {

  8.     // 共享数据
  9.     private int shareValue = -1;

  10.     // 是否已经存储
  11.     private boolean isStored = false;

  12.     // 对象锁
  13.     private final byte[] lock = new byte[0];

  14.     public void setShareValue(int value) {
  15.         synchronized (lock) {
  16.             while (isStored) {
  17.                 waitForOperation();
  18.             }
  19.             shareValue = value;
  20.             isStored = true;
  21.             lock.notifyAll();
  22.         }
  23.     }

  24.     public int getShareValue() {
  25.         synchronized (lock) {
  26.             while (!isStored) {
  27.                 waitForOperation();
  28.             }
  29.             isStored = false;
  30.             lock.notifyAll();
  31.             return shareValue;
  32.         }
  33.     }

  34.     private void waitForOperation() {
  35.         try {
  36.             lock.wait();
  37.         } catch (InterruptedException e) {
  38.             System.out.println(e.toString());
  39.         }
  40.     }

  41. }

点击(此处)折叠或打开

  1. /**
  2.  * 测试类
  3.  *
  4.  * @author renwei
  5.  *
  6.  */
  7. public class Client {

  8.     public static void main(String[] args) {
  9.         final ShareArea shareArea = new ShareArea();
  10.         final Consumer consumer = new Consumer(shareArea);
  11.         final Producer producer = new Producer(shareArea);
  12.         producer.start();
  13.         consumer.start();
  14.     }

  15. }

执行结果:

Producer put a value: 368
Consumer get a value: 368

Producer put a value: 429
Consumer get a value: 429

Producer put a value: 154
Consumer get a value: 154

Producer put a value: 412
Consumer get a value: 412

Producer put a value: 69
Consumer get a value: 69

Producer put a value: 348
Consumer get a value: 348

Producer put a value: 403
Consumer get a value: 403

Producer put a value: 247
Consumer get a value: 247

Producer put a value: 437
Consumer get a value: 437

Producer put a value: 408
Consumer get a value: 408

Producer put a value: 13
Consumer get a value: 13

Producer put a value: 314
Consumer get a value: 314


 

阅读(2030) | 评论(1) | 转发(0) |
0

上一篇:观察者模式

下一篇:eXosip编译

给主人留下些什么吧!~~