-信号量(Semaphore)在生产者和消费者模式的使用

转自:  http://blog.csdn.net/java2000_net/article/details/3997449

Semaphore 信号量,就是一个允许实现设置好的令牌。也许有1个,也许有10个或更多。 
谁拿到令牌(acquire)就可以去执行了,如果没有令牌则需要等待。 
执行完毕,一定要归还(release)令牌,否则令牌会被很快用光,别的线程就无法获得令牌而执行下去了。

 

请仔细体会里面关于仓库的处理,

1 是如何保证入库时,如果仓库满就等待,

2 出库时,如果仓库无货就等待的。

3 以及对仓库只有10个库位的处理。

4 对同步问题的处理。

 

 

[java]  view plain copy
  1. import java.util.concurrent.Semaphore;  
  2. /** 
  3.  * 老紫竹JAVA提高教程-信号量(Semaphore)的使用。<br> 
  4.  * 生产者和消费者的例子,库存的管理。 
  5.  *  
  6.  * @author 老紫竹(java2000.net,laozizhu.com) 
  7.  */  
  8. public class TestSemaphore {  
  9.   public static void main(String[] args) {  
  10.     // 启动线程  
  11.     for (int i = 0; i <= 3; i++) {  
  12.       // 生产者  
  13.       new Thread(new Producer()).start();  
  14.       // 消费者  
  15.       new Thread(new Consumer()).start();  
  16.     }  
  17.   }  
  18.   // 仓库  
  19.   static Warehouse buffer = new Warehouse();  
  20.   // 生产者,负责增加  
  21.   static class Producer implements Runnable {  
  22.     static int num = 1;  
  23.     @Override  
  24.     public void run() {  
  25.       int n = num++;  
  26.       while (true) {  
  27.         try {  
  28.           buffer.put(n);  
  29.           System.out.println(">" + n);  
  30.           // 速度较快。休息10毫秒  
  31.           Thread.sleep(10);  
  32.         } catch (InterruptedException e) {  
  33.           e.printStackTrace();  
  34.         }  
  35.       }  
  36.     }  
  37.   }  
  38.   // 消费者,负责减少  
  39.   static class Consumer implements Runnable {  
  40.     @Override  
  41.     public void run() {  
  42.       while (true) {  
  43.         try {  
  44.           System.out.println("<" + buffer.take());  
  45.           // 速度较慢,休息1000毫秒  
  46.           Thread.sleep(1000);  
  47.         } catch (InterruptedException e) {  
  48.           e.printStackTrace();  
  49.         }  
  50.       }  
  51.     }  
  52.   }  
  53.   /** 
  54.    * 仓库 
  55.    *  
  56.    * @author 老紫竹(laozizhu.com) 
  57.    */  
  58.   static class Warehouse {  
  59.     // 非满锁  
  60.     final Semaphore notFull = new Semaphore(10);  
  61.     // 非空锁  
  62.     final Semaphore notEmpty = new Semaphore(0);  
  63.     // 核心锁  
  64.     final Semaphore mutex = new Semaphore(1);  
  65.     // 库存容量  
  66.     final Object[] items = new Object[10];  
  67.     int putptr, takeptr, count;  
  68.     /** 
  69.      * 把商品放入仓库.<br> 
  70.      *  
  71.      * @param x 
  72.      * @throws InterruptedException 
  73.      */  
  74.     public void put(Object x) throws InterruptedException {  
  75.       // 保证非满  
  76.       notFull.acquire();  
  77.       // 保证不冲突  
  78.       mutex.acquire();  
  79.       try {  
  80.         // 增加库存  
  81.         items[putptr] = x;  
  82.         if (++putptr == items.length)  
  83.           putptr = 0;  
  84.         ++count;  
  85.       } finally {  
  86.         // 退出核心区  
  87.         mutex.release();  
  88.         // 增加非空信号量,允许获取商品  
  89.         notEmpty.release();  
  90.       }  
  91.     }  
  92.     /** 
  93.      * 从仓库获取商品 
  94.      *  
  95.      * @return 
  96.      * @throws InterruptedException 
  97.      */  
  98.     public Object take() throws InterruptedException {  
  99.       // 保证非空  
  100.       notEmpty.acquire();  
  101.       // 核心区  
  102.       mutex.acquire();  
  103.       try {  
  104.         // 减少库存  
  105.         Object x = items[takeptr];  
  106.         if (++takeptr == items.length)  
  107.           takeptr = 0;  
  108.         --count;  
  109.         return x;  
  110.       } finally {  
  111.         // 退出核心区  
  112.         mutex.release();  
  113.         // 增加非满的信号量,允许加入商品  
  114.         notFull.release();  
  115.       }  
  116.     }  
  117.   }  
  118. }  

更多 0
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值