Java 併發編程學習筆記(3) ----Semaphore-tryAcquire()的使用

tryAcquire()的使用

參數使用

當前時刻
    
    tryAcquire(int permits)
    Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.
    嘗試去從這個信號量獲取指定數量的在調用時都是可用的許可 。
    如果不使用permits 參數,tryAcquire()表示獲取一個許可。

指定時間
    
    tryAcquire(int permits, long timeout, TimeUnit unit)
    Acquires the given number of permits from this semaphore,
    if all become available within the given waiting time and the current thread has not been interrupted.
    在指定的時間內嘗試去從這個信號量獲取指定數量的許可 ,同時這段時間內,這個線程沒有被中斷。
    如果不使用permits 參數,tryAcquire(long timeout, TimeUnit unit)表示獲取一個許可。  

代碼


package com.lhc.concurrent.semaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class TryAcquireService {
    private Semaphore semaphore = new Semaphore(8);

    public void doSomething() {
        try {
            /**
             * tryAcquire(int permits)
             * Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.
             * 嘗試去從這個信號量獲取指定數量的在調用時都是可用的許可 。
             * 如果不使用permits 參數,tryAcquire()表示獲取一個許可。
             */
            if (semaphore.tryAcquire(2)) {
                System.out.println(Thread.currentThread().getName() + "獲得鎖,時間:" + System.currentTimeMillis());
                Thread.sleep(100);
                semaphore.release(2);
            }else {
                System.out.println(Thread.currentThread().getName() + "沒有獲得鎖,時間:" + System.currentTimeMillis());
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public void doThing() {
        try {
            /**
             * tryAcquire(int permits, long timeout, TimeUnit unit)
             * Acquires the given number of permits from this semaphore,
             * if all become available within the given waiting time and the current thread has not been interrupted.
             * 在指定的時間內嘗試去從這個信號量獲取指定數量的許可 ,同時這段時間內,這個線程沒有被中斷。
             * 如果不使用permits 參數,tryAcquire(long timeout, TimeUnit unit)表示獲取一個許可。
             */
            if (semaphore.tryAcquire(2, 1, TimeUnit.SECONDS)) {
                System.out.println(Thread.currentThread().getName() + "獲得鎖,時間:" + System.currentTimeMillis());
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "釋放鎖,時間:" + System.currentTimeMillis());
                semaphore.release(2);
            }else {
                System.out.println(Thread.currentThread().getName() + "沒有獲得鎖,時間:" + System.currentTimeMillis());
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

測試類


package com.lhc.concurrent.semaphore;

public class TryAcquireThread extends Thread{
    private TryAcquireService tryAcquireService;

    public TryAcquireThread(TryAcquireService tryAcquireService, String name) {
        super();
        this.tryAcquireService = tryAcquireService;
        this.setName(name);
    }

    public static void main(String[] args){
        TryAcquireService tryAcquireService = new TryAcquireService();
        for (int i = 0; i < 10; i++) {
            TryAcquireThread tryAcquireThread = new TryAcquireThread(tryAcquireService, "線程" + i);
            tryAcquireThread.start();
        }
    }

    @Override
    public void run() {
        //tryAcquireService.doSomething();
        tryAcquireService.doThing();
    }
}


測試結果

獲取當前時刻

線程0獲得鎖,時間:1555145916044
線程4沒有獲得鎖,時間:1555145916044
線程2獲得鎖,時間:1555145916044
線程1獲得鎖,時間:1555145916044
線程6沒有獲得鎖,時間:1555145916044
線程5沒有獲得鎖,時間:1555145916044
線程3獲得鎖,時間:1555145916044
線程7沒有獲得鎖,時間:1555145916044
線程8沒有獲得鎖,時間:1555145916044
線程9沒有獲得鎖,時間:1555145916044

獲取指定時間內

線程9獲得鎖,時間:1555146046722
線程7獲得鎖,時間:1555146046722
線程1獲得鎖,時間:1555146046722
線程6獲得鎖,時間:1555146046722
線程6釋放鎖,時間:1555146047722
線程9釋放鎖,時間:1555146047722
線程5獲得鎖,時間:1555146047722
線程4獲得鎖,時間:1555146047722
線程3沒有獲得鎖,時間:1555146047722
線程1釋放鎖,時間:1555146047722
線程7釋放鎖,時間:1555146047722
線程0沒有獲得鎖,時間:1555146047722
線程8沒有獲得鎖,時間:1555146047722
線程2沒有獲得鎖,時間:1555146047722
線程5釋放鎖,時間:1555146048723
線程4釋放鎖,時間:1555146048723

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章