Semaphore,動態增減訊號量

來源:互聯網
上載者:User

我的理解~~

【訊號量】:

  • 用於控制對某資源訪問的同一時間的並發量。

 

【如何擷取】:

  • semaphore.tryAcquire(),嘗試擷取,不阻塞
  • semaphore.acquire(),沒訊號量可用時,將進行阻塞等

 

【如何釋放】:

  • semaphore.release();
  • 線程拋出各種異常,都別忘了在finally中釋放訊號量;
  • 如果釋放的比擷取的訊號量還多,例如擷取了2個,釋放了5次,那麼當前訊號量就動態增加為5了,要注意。

 

【動態增加】:

  • 多釋放幾次,就可以達到訊號量動態增加的效果了

 

【動態減小】:

  • 訊號量本來有這個api的,不過是protected方法,所以我們需要顯式繼續Semaphore,並重新實現該api,見ResizeableSemaphore類中的reducePermits(int reduction);
  • 舉例如下:(該表格有個假設前提,不存在多餘的release而產生出新的訊號量,即release次數<=acquire次數)
當前訊號量A
(A=A1+A2)
佔用訊號量A1 可用訊號量A2 重新設定訊號量B
(B=B1+B2)
當前可用的訊號量B1 當前待釋放的量B2
5 3 2 3 0 0
5 3 2 1 0 -2
5 3 2 9 6 0

 

 

【動態減小—demo】:

import java.util.concurrent.Semaphore;

 

 

public class AdjustableSemaphore {

 

    private final ResizeableSemaphore  semaphore = new ResizeableSemaphore();

    

    private int maxPermits = 0;

    

    public AdjustableSemaphore(){

        

    }

    

    synchronized void setMaxPermits(int newMax){

        if(newMax < 1){

            throw new IllegalArgumentException("Semaphore size must be at least 1,"

                + " was " + newMax);

        }

        

        int delta = newMax - this.maxPermits;

        

        if(delta == 0){

            return ;

        }else if(delta > 0){

            this.semaphore.release(delta);

        }else {

            delta *= -1;

            this.semaphore.reducePermits(delta);

        }

        

        this.maxPermits = newMax;

        

    }

    

    public int availablePermits(){

       return this.semaphore.availablePermits();

    }

    

    public void release(){

        this.semaphore.release();

    }

    

    public boolean tryAcquire(){

        return this.semaphore.tryAcquire();

    }

    

    

    

    

    private static final class ResizeableSemaphore extends Semaphore {

 

        ResizeableSemaphore(){

            super(0);

        }

        

        @Override

        protected void reducePermits(int reduction){

            super.reducePermits(reduction);

        }

        

    }

    

    

}
 
 

【測試程式】:

AdjustableSemaphore semaphore = new AdjustableSemaphore();

        System.out.println("==============5");

        semaphore.setMaxPermits(5);

        

        for(int i=0;i<20;i++){

            semaphore.tryAcquire();

        }

        

        

        System.out.println(semaphore.availablePermits()); //5個訊號量全被佔用,所以當前可用的為0

        

        

        System.out.println("==============2");

        semaphore.setMaxPermits(2);

        

        System.out.println(semaphore.availablePermits()); //將訊號量顯式設定為2,與上一步合并結果(2-5)=-3,表示目前有5個被佔用,訊號量只有2,所以還有3個欠著待釋放

        

        

        System.out.println("==============20");

        semaphore.setMaxPermits(20);

        System.out.println(semaphore.availablePermits());//將訊號量顯式設定為20,與上一步合并結果(20-2)+(-3)=15個,表示目前還有15個可用。

        

        System.out.println("==============3");

        semaphore.setMaxPermits(3);

        System.out.println(semaphore.availablePermits());//同上,(3-20)+15=-2

        

        System.out.println("==============1");

        semaphore.setMaxPermits(1);

        System.out.println(semaphore.availablePermits());//同上,(1-3)-2=-4

        

        System.out.println("==============10");

        semaphore.setMaxPermits(10);

        System.out.println(semaphore.availablePermits());//同上,(10-1)-4=5

        

        System.out.println("==============FINALLY");

        for(int i=0;i<7;i++){

            semaphore.release();

        }

        System.out.println(semaphore.availablePermits());//釋放了7個,所以7+5=12,雖然顯式設定了訊號量為10,但因多release()了兩次,所以無意之中隱式增大了訊號量。
 

 
 

【輸出結果】:

==============5
0
==============2
-3
==============20
15
==============3
-2
==============1
-4
==============10
5
==============FINALLY
10

 

這裡可參考:http://blog.teamlazerbeez.com/2009/04/20/javas-semaphore-resizing/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.