Java信號量全解析
Semaphore(信號量) 是一個線程同步結構,用于在線程間傳遞信號,以避免出現信號丟失(譯者注:下文會具體介紹),或者像鎖一樣用于保護一個關鍵區域。自從5.0開始,jdk在java.util.concurrent包里提供了Semaphore 的官方實現,因此大家不需要自己去實現Semaphore。但是還是很有必要去熟悉如何使用Semaphore及其背后的原理
內容主題:一、簡單的Semaphore實現下面是一個信號量的簡單實現:
public class Semaphore {private boolean signal = false;public synchronized void take() {this.signal = true;this.notify();1011}public synchronized void release() throws InterruptedException{while(!this.signal) wait();this.signal = false;}}
Take方法發出一個被存放在Semaphore內部的信號,而Release方法則等待一個信號,當其接收到信號后,標記位signal被清空,然后該方法終止。
使用這個semaphore可以避免錯失某些信號通知。用take方法來代替notify,release方法來代替wait。如果某線程在調用release等待之前調用take方法,那么調用release方法的線程仍然知道take方法已經被某個線程調用過了,因為該Semaphore內部保存了take方法發出的信號。而wait和notify方法就沒有這樣的功能。
當用semaphore來產生信號時,take和release這兩個方法名看起來有點奇怪。這兩個名字來源于后面把semaphore當做鎖的例子,后面會詳細介紹這個例子,在該例子中,take和release這兩個名字會變得很合理。
二、使用Semaphore來產生信號下面的例子中,兩個線程通過Semaphore發出的信號來通知對方
Semaphore semaphore = new Semaphore();SendingThread sender = new SendingThread(semaphore);ReceivingThread receiver = new ReceivingThread(semaphore);receiver.start();sender.start();public class SendingThread {Semaphore semaphore = null;public SendingThread(Semaphore semaphore){this.semaphore = semaphore;}public void run(){while(true){//do something, then signalthis.semaphore.take();}}}public class RecevingThread {Semaphore semaphore = null;public ReceivingThread(Semaphore semaphore){this.semaphore = semaphore;}public void run(){while(true){this.semaphore.release();//receive signal, then do something...}}}三、可計數的Semaphore
上面提到的Semaphore的簡單實現并沒有計算通過調用take方法所產生信號的數量。可以把它改造成具有計數功能的Semaphore。下面是一個可計數的Semaphore的簡單實現。
public class CountingSemaphore {private int signals = 0;public synchronized void take() {this.signals++;0809this.notify();}public synchronized void release() throws InterruptedException{while(this.signals == 0) wait();this.signals--;}}四、有上限的Semaphore
上面的CountingSemaphore并沒有限制信號的數量。下面的代碼將CountingSemaphore改造成一個信號數量有上限的BoundedSemaphore。
public class BoundedSemaphore {private int signals = 0;private int bound = 0;public BoundedSemaphore(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();}}
在BoundedSemaphore中,當已經產生的信號數量達到了上限,take方法將阻塞新的信號產生請求,直到某個線程調用release方法后,被阻塞于take方法的線程才能傳遞自己的信號。
五、把Semaphore當鎖來使用當信號量的數量上限是1時,Semaphore可以被當做鎖來使用。通過take和release方法來保護關鍵區域。請看下面的例子:
BoundedSemaphore semaphore = new BoundedSemaphore(1);...semaphore.take();try{//critical section} finally {semaphore.release();}
在前面的例子中,Semaphore被用來在多個線程之間傳遞信號,這種情況下,take和release分別被不同的線程調用。但是在鎖這個例子中,take和release方法將被同一線程調用,因為只允許一個線程來獲取信號(允許進入關鍵區域的信號),其它調用take方法獲取信號的線程將被阻塞,知道第一個調用take方法的線程調用release方法來釋放信號。對release方法的調用永遠不會被阻塞,這是因為任何一個線程都是先調用take方法,然后再調用release。
通過有上限的Semaphore可以限制進入某代碼塊的線程數量。設想一下,在上面的例子中,如果BoundedSemaphore 上限設為5將會發生什么?意味著允許5個線程同時訪問關鍵區域,但是你必須保證,這個5個線程不會互相沖突。否則你的應用程序將不能正常運行。
必須注意,release方法應當在finally塊中被執行。這樣可以保在關鍵區域的代碼拋出異常的情況下,信號也一定會被釋放。
以上就是Java信號量全解析的詳細內容,更多關于Java信號量的資料請關注好吧啦網其它相關文章!
相關文章: