Java中信号量 Semaphore

Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目。自从5.0开始,jdk在java.util.concurrent包里提供了Semaphore 的官方实现,因此大家不需要自己去实现Semaphore


下面的类使用信号量控制对内容池的访问:

Java代码   收藏代码
  1. import java.util.concurrent.Semaphore;  
  2. class Pool {  
  3.     private static final int MAX_AVAILABLE = 100;  
  4.     private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);  
  5.     public Object getItem() throws InterruptedException {  
  6.         available.acquire(); // 从此信号量获取一个许可,在提供一个许可前一直将线程阻塞,否则线程被中断  
  7.         return getNextAvailableItem();  
  8.     }  
  9.     public void putItem(Object x) {  
  10.         if (markAsUnused(x))  
  11.             available.release(); // 释放一个许可,将其返回给信号量  
  12.     }  
  13.     // 仅作示例参考,非真实数据  
  14.     protected Object[] items = null;  
  15.     protected boolean[] used = new boolean[MAX_AVAILABLE];  
  16.     protected synchronized Object getNextAvailableItem() {  
  17.         for (int i = 0; i < MAX_AVAILABLE; ++i) {  
  18.             if (!used[i]) {  
  19.                 used[i] = true;  
  20.                 return items[i];  
  21.             }  
  22.         }  
  23.         return null;  
  24.     }  
  25.     protected synchronized boolean markAsUnused(Object item) {  
  26.         for (int i = 0; i < MAX_AVAILABLE; ++i) {  
  27.             if (item == items[i]) {  
  28.                 if (used[i]) {  
  29.                     used[i] = false;  
  30.                     return true;  
  31.                 } else  
  32.                     return false;  
  33.             }  
  34.         }  
  35.         return false;  
  36.     }  
  37. }  

 

虽然JDK已经提供了相关实现,但是还是很有必要去熟悉如何使用Semaphore及其背后的原理。
做一个简单的Semaphore实现

Java代码   收藏代码
  1. class SemaphoreTest {  
  2.     private boolean signal = false;  
  3.     public synchronized void take() {  
  4.         this.signal = true;  
  5.         this.notify();  
  6.     }  
  7.     public synchronized void release() throws InterruptedException {  
  8.         while (!this.signal)  
  9.             wait();  
  10.         this.signal = false;  
  11.     }  
  12. }  

 

使用这个semaphore可以避免错失某些信号通知。用take方法来代替notify,release方法来代替wait。如果某线程在调用release等待之前调用take方法,那么调用release方法的线程仍然知道take方法已经被某个线程调用过了,因为该Semaphore内部保存了take方法发出的信号。而wait和notify方法就没有这样的功能。

 

可计数的Semaphore

Java代码   收藏代码
  1. class SemaphoreTest {  
  2.     private int signals = 0;  
  3.     public synchronized void take() {  
  4.         this.signals++;  
  5.         this.notify();  
  6.     }  
  7.     public synchronized void release() throws InterruptedException {  
  8.         while (this.signals == 0)  
  9.             wait();  
  10.         this.signals--;  
  11.     }  
  12. }  

 

Semaphore上限

Java代码   收藏代码
  1. class SemaphoreTest {  
  2.     private int signals = 0;  
  3.     private int bound = 0;  
  4.     public SemaphoreTest(int upperBound) {  
  5.         this.bound = upperBound;  
  6.     }  
  7.     public synchronized void take() throws InterruptedException {  
  8.         while (this.signals == bound)  
  9.             wait();  
  10.         this.signals++;  
  11.         this.notify();  
  12.     }  
  13.     public synchronized void release() throws InterruptedException {  
  14.         while (this.signals == 0)  
  15.             wait();  
  16.         this.signals--;  
  17.         this.notify();  
  18.     }  
  19. }  

 

当已经产生的信号数量达到了上限,take方法将阻塞新的信号产生请求,直到某个线程调用release方法后,被阻塞于take方法的线程才能传递自己的信号。

 

把Semaphore当锁来使用
当信号量的数量上限是1时,Semaphore可以被当做锁来使用。通过take和release方法来保护关键区域。

 

请您到ITEYE网站看 java小强 原创,谢谢!

http://cuisuqiang.iteye.com/ !

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值