java中的信号量semaphore实现生产者消费者模式

来源:互联网 发布:mac usb设备已停用 编辑:程序博客网 时间:2024/05/17 18:04

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

 

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

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

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

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

4 对同步问题的处理。

 

 

[java] view plaincopy
  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. }  

转载出处:http://blog.csdn.net/java2000_net/article/details/3997449

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 学校更名了盖章怎么办 工商注册资金没有交怎么办 住公司宿舍怎么办居住证 住在公司宿舍怎么办居住证 滴滴没有的车型怎么办 假的租房合同怎么办 代注册公司被骗怎么办 公司跨区迁址怎么办 公司搬走注册地怎么办 注册公司没有房产证怎么办 公司不运营了怎么办 公司注销了账户怎么办 租户不变更地址怎么办 营业执照忘审了怎么办 工商营业执照年检过期怎么办 个体营业执照没有年报怎么办? 个体工商户一年没有申报怎么办 个体工商户逾期未申报怎么办 个体户没报税过怎么办 农业银行证书过期了怎么办 ca证书丢了怎么办 ca证书被锁怎么办 上个月忘记清卡怎么办 财务人员进入税务黑名单怎么办 社保本丢了怎么办 贷款车辆登记证书怎么办 发票薄丢了怎么办? 汽车发票丢了怎么办 税票弄丢了怎么办 交强险正本丢了怎么办 个体营业执照正本丢失怎么办 简易注销后税务怎么办 拒绝了日历邀请怎么办 老人走丢了怎么办 老人走丢找不到怎么办 没人给介绍对象怎么办 bate365账号被锁怎么办 qq号疑似被盗怎么办 不知道音乐名字怎么办 忘记支付宝登录怎么办 微信被老婆拉黑怎么办