软件编程
位置:首页>> 软件编程>> java编程>> Java信号量Semaphore原理及代码实例

Java信号量Semaphore原理及代码实例

作者:cuisuqiang  发布时间:2021-10-02 21:55:08 

标签:Java,信号量,Semaphore

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

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


import java.util.concurrent.Semaphore;
class Pool {
 private static final int MAX_AVAILABLE = 100;
 private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
 public Object getItem() throws InterruptedException {
   available.acquire(); // 从此信号量获取一个许可,在提供一个许可前一直将线程阻塞,否则线程被中断
   return getNextAvailableItem();
 }
 public void putItem(Object x) {
   if (markAsUnused(x))
     available.release(); // 释放一个许可,将其返回给信号量
 }
 // 仅作示例参考,非真实数据
 protected Object[] items = null;
 protected boolean[] used = new boolean[MAX_AVAILABLE];
 protected synchronized Object getNextAvailableItem() {
   for (int i = 0; i < MAX_AVAILABLE; ++i) {
     if (!used[i]) {
       used[i] = true;
       return items[i];
     }
   }
   return null;
 }
 protected synchronized boolean markAsUnused(Object item) {
   for (int i = 0; i < MAX_AVAILABLE; ++i) {
     if (item == items[i]) {
       if (used[i]) {
         used[i] = false;
         return true;
       } else
         return false;
     }
   }
   return false;
 }
}

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


class SemaphoreTest {
 private boolean signal = false;
 public synchronized void take() {
   this.signal = true;
   this.notify();
 }
 public synchronized void release() throws InterruptedException {
   while (!this.signal)
     wait();
   this.signal = false;
 }
}

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

可计数的Semaphore:


class SemaphoreTest {
 private int signals = 0;
 public synchronized void take() {
   this.signals++;
   this.notify();
 }
 public synchronized void release() throws InterruptedException {
   while (this.signals == 0)
     wait();
   this.signals--;
 }
}

Semaphore上限:


class SemaphoreTest {
private int signals = 0;
private int bound = 0;
public SemaphoreTest(int upperBound) {
this.bound = upperBound;
}
public synchronized void take() throws InterruptedException {
while (this.signals == bound)
wait();
this.signals++;
this.notify();
}
public synchronized void release() throws InterruptedException {
while (this.signals == 0)
wait();
this.signals--;
this.notify();
}
}

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

把Semaphore当锁来使用:

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

来源:https://www.iteye.com/blog/cuisuqiang-2020146

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com