Java生产者消费者例子

生产者-消费者(producer-consumer)问题,也称作有界缓冲区(bounded-buffer)问题,两个进程共享一个公共的固定大小的缓冲区。其中一个是生产者,用于将消息放入缓冲区;另外一个是消费者,用于从缓冲区中取出消息。问题出现在当缓冲区已经满了,而此时生产者还想向其中放入一个新的数据项的情形,其解决方法是让生产者此时进行休眠,等待消费者从缓冲区中取走了一个或者多个数据后再去唤醒它。同样地,当缓冲区已经空了,而消费者还想去取消息,此时也可以让消费者进行休眠,等待生产者放入一个或者多个数据时再唤醒它。

一,首先定义公共资源类,其中的变量number是保存的公共数据。并且定义两个方法,增加number的值和减少number的值。由于多线程的原因,必须加上synchronized关键字,注意while判断的条件。

Java代码   收藏代码
  1. /** 
  2.  * 公共资源类 
  3.  */  
  4. public class PublicResource {  
  5.     private int number = 0;  
  6.   
  7.     /** 
  8.      * 增加公共资源 
  9.      */  
  10.     public synchronized void increace() {  
  11.         while (number != 0) {  
  12.             try {  
  13.                 wait();  
  14.             } catch (InterruptedException e) {  
  15.                 e.printStackTrace();  
  16.             }  
  17.         }  
  18.         number++;  
  19.         System.out.println(number);  
  20.         notify();  
  21.     }  
  22.   
  23.     /** 
  24.      * 减少公共资源 
  25.      */  
  26.     public synchronized void decreace() {  
  27.         while (number == 0) {  
  28.             try {  
  29.                 wait();  
  30.             } catch (InterruptedException e) {  
  31.                 e.printStackTrace();  
  32.             }  
  33.         }  
  34.         number--;  
  35.         System.out.println(number);  
  36.         notify();  
  37.     }  
  38. }  
 

二,分别定义生产者线程和消费者线程,并模拟多次生产和消费,即增加和减少公共资源的number值

Java代码   收藏代码
  1. /** 
  2.  * 生产者线程,负责生产公共资源 
  3.  */  
  4. public class ProducerThread implements Runnable {  
  5.     private PublicResource resource;  
  6.   
  7.     public ProducerThread(PublicResource resource) {  
  8.         this.resource = resource;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         for (int i = 0; i < 10; i++) {  
  14.             try {  
  15.                 Thread.sleep((long) (Math.random() * 1000));  
  16.             } catch (InterruptedException e) {  
  17.                 e.printStackTrace();  
  18.             }  
  19.             resource.increace();  
  20.         }  
  21.     }  
  22. }  
  23. /** 
  24.  * 消费者线程,负责消费公共资源 
  25.  */  
  26. public class ConsumerThread implements Runnable {  
  27.     private PublicResource resource;  
  28.   
  29.     public ConsumerThread(PublicResource resource) {  
  30.         this.resource = resource;  
  31.     }  
  32.   
  33.     @Override  
  34.     public void run() {  
  35.         for (int i = 0; i < 10; i++) {  
  36.             try {  
  37.                 Thread.sleep((long) (Math.random() * 1000));  
  38.             } catch (InterruptedException e) {  
  39.                 e.printStackTrace();  
  40.             }  
  41.             resource.decreace();  
  42.         }  
  43.     }  
  44. }  
 

三,模拟多个生产者和消费者操作公共资源的情形,结果须保证是在允许的范围内。

Java代码   收藏代码
  1. public class ProducerConsumerTest {  
  2.     public static void main(String[] args) {  
  3.         PublicResource resource = new PublicResource();  
  4.         new Thread(new ProducerThread(resource)).start();  
  5.         new Thread(new ConsumerThread(resource)).start();  
  6.         new Thread(new ProducerThread(resource)).start();  
  7.         new Thread(new ConsumerThread(resource)).start();  
  8.         new Thread(new ProducerThread(resource)).start();  
  9.         new Thread(new ConsumerThread(resource)).start();  
  10.     }  
  11. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值