• <menu id="gyiem"><menu id="gyiem"></menu></menu>
  • <menu id="gyiem"><code id="gyiem"></code></menu>

    Java進階(三)多線程開發關鍵技術

    原創文章,轉載請務必將下面這段話置于文章開頭處(保留超鏈接)。
    本文轉發自技術世界原文鏈接 http://www.luozeyang.com/java/multi_thread/

    sleep和wait到底什么區別

    其實這個問題應該這么問——sleep和wait有什么相同點。因為這兩個方法除了都能讓當前線程暫停執行完,幾乎沒有其它相同點。

    wait方法是Object類的方法,這意味著所有的Java類都可以調用該方法。sleep方法是Thread類的靜態方法。

    wait是在當前線程持有wait對象鎖的情況下,暫時放棄鎖,并讓出CPU資源,并積極等待其它線程調用同一對象的notify或者notifyAll方法。注意,即使只有一個線程在等待,并且有其它線程調用了notify或者notifyAll方法,等待的線程只是被激活,但是它必須得再次獲得鎖才能繼續往下執行。換言之,即使notify被調用,但只要鎖沒有被釋放,原等待線程因為未獲得鎖仍然無法繼續執行。測試代碼如下所示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    import java.util.Date;

    public class Wait {

    public static void main(String[] args) {
    Thread thread1 = new Thread(() -> {
    synchronized (Wait.class) {
    try {
    System.out.println(new Date() + " Thread1 is running");
    Wait.class.wait();
    System.out.println(new Date() + " Thread1 ended");
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    });
    thread1.start();

    Thread thread2 = new Thread(() -> {
    synchronized (Wait.class) {
    try {
    System.out.println(new Date() + " Thread2 is running");
    Wait.class.notify();
    // Don't use sleep method to avoid confusing
    for(long i = 0; i < 200000; i++) {
    for(long j = 0; j < 100000; j++) {}
    }
    System.out.println(new Date() + " Thread2 release lock");
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    for(long i = 0; i < 200000; i++) {
    for(long j = 0; j < 100000; j++) {}
    }
    System.out.println(new Date() + " Thread2 ended");
    });

    // Don't use sleep method to avoid confusing
    for(long i = 0; i < 200000; i++) {
    for(long j = 0; j < 100000; j++) {}
    }
    thread2.start();
    }
    }

    執行結果如下

    1
    2
    3
    4
    5
    Tue Jun 14 22:51:11 CST 2016 Thread1 is running
    Tue Jun 14 22:51:23 CST 2016 Thread2 is running
    Tue Jun 14 22:51:36 CST 2016 Thread2 release lock
    Tue Jun 14 22:51:36 CST 2016 Thread1 ended
    Tue Jun 14 22:51:49 CST 2016 Thread2 ended

    從運行結果可以看出

    • thread1執行wait后,暫停執行
    • thread2執行notify后,thread1并沒有繼續執行,因為此時thread2尚未釋放鎖,thread1因為得不到鎖而不能繼續執行
    • thread2執行完synchronized語句塊后釋放鎖,thread1得到通知并獲得鎖,進而繼續執行

    注意:wait方法需要釋放鎖,前提條件是它已經持有鎖。所以wait和notify(或者notifyAll)方法都必須被包裹在synchronized語句塊中,并且synchronized后鎖的對象應該與調用wait方法的對象一樣。否則拋出IllegalMonitorStateException

    sleep方法告訴操作系統至少指定時間內不需為線程調度器為該線程分配執行時間片,并不釋放鎖(如果當前已經持有鎖)。實際上,調用sleep方法時并不要求持有任何鎖。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    package com.test.thread;

    import java.util.Date;

    public class Sleep {

    public static void main(String[] args) {
    Thread thread1 = new Thread(() -> {
    synchronized (Sleep.class) {
    try {
    System.out.println(new Date() + " Thread1 is running");
    Thread.sleep(2000);
    System.out.println(new Date() + " Thread1 ended");
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    });
    thread1.start();

    Thread thread2 = new Thread(() -> {
    synchronized (Sleep.class) {
    try {
    System.out.println(new Date() + " Thread2 is running");
    Thread.sleep(2000);
    System.out.println(new Date() + " Thread2 ended");
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    for(long i = 0; i < 200000; i++) {
    for(long j = 0; j < 100000; j++) {}
    }
    });

    // Don't use sleep method to avoid confusing
    for(long i = 0; i < 200000; i++) {
    for(long j = 0; j < 100000; j++) {}
    }
    thread2.start();
    }
    }

    執行結果如下

    1
    2
    3
    4
    Thu Jun 16 19:46:06 CST 2016 Thread1 is running
    Thu Jun 16 19:46:08 CST 2016 Thread1 ended
    Thu Jun 16 19:46:13 CST 2016 Thread2 is running
    Thu Jun 16 19:46:15 CST 2016 Thread2 ended

    由于thread 1和thread 2的run方法實現都在同步塊中,無論哪個線程先拿到鎖,執行sleep時并不釋放鎖,因此其它線程無法執行。直到前面的線程sleep結束并退出同步塊(釋放鎖),另一個線程才得到鎖并執行。

    注意:sleep方法并不需要持有任何形式的鎖,也就不需要包裹在synchronized中。

    本文所有示例均基于Java HotSpot(TM) 64-Bit Server VM

    調用sleep方法的線程,在jstack中顯示的狀態為sleeping

    1
    java.lang.Thread.State: TIMED_WAITING (sleeping)

    調用wait方法的線程,在jstack中顯示的狀態為on object monitor

    1
    java.lang.Thread.State: WAITING (on object monitor)

    synchronized幾種用法

    每個Java對象都可以用做一個實現同步的互斥鎖,這些鎖被稱為內置鎖。線程進入同步代碼塊或方法時自動獲得內置鎖,退出同步代碼塊或方法時自動釋放該內置鎖。進入同步代碼塊或者同步方法是獲得內置鎖的唯一途徑。

    實例同步方法

    synchronized用于修飾實例方法(非靜態方法)時,執行該方法需要獲得的是該類實例對象的內置鎖(同一個類的不同實例擁有不同的內置鎖)。如果多個實例方法都被synchronized修飾,則當多個線程調用同一實例的不同同步方法(或者同一方法)時,需要競爭鎖。但當調用的是不同實例的方法時,并不需要競爭鎖。

    靜態同步方法

    synchronized用于修飾靜態方法時,執行該方法需要獲得的是該類的class對象的內置鎖(一個類只有唯一一個class對象)。調用同一個類的不同靜態同步方法時會產生鎖競爭。

    同步代碼塊

    synchronized用于修飾代碼塊時,進入同步代碼塊需要獲得synchronized關鍵字后面括號內的對象(可以是實例對象也可以是class對象)的內置鎖。

    synchronized使用總結

    鎖的使用是為了操作臨界資源的正確性,而往往一個方法中并非所有的代碼都操作臨界資源。換句話說,方法中的代碼往往并不都需要同步。此時建議不使用同步方法,而使用同步代碼塊,只對操作臨界資源的代碼,也即需要同步的代碼加鎖。這樣做的好處是,當一個線程在執行同步代碼塊時,其它線程仍然可以執行該方法內同步代碼塊以外的部分,充分發揮多線程并發的優勢,從而相較于同步整個方法而言提升性能。

    釋放Java內置鎖的唯一方式是synchronized方法或者代碼塊執行結束。若某一線程在synchronized方法或代碼塊內發生死鎖,則對應的內置鎖無法釋放,其它線程也無法獲取該內置鎖(即進入跟該內置鎖相關的synchronized方法或者代碼塊)。

    使用jstack dump線程棧時,可查看到相關線程通過synchronized獲取到或等待的對象,但Locked ownable synchronizers仍然顯示為None。下例中,線程thead-test-b已獲取到類型為java.lang.Double的對象的內置鎖(monitor),且該對象的內存地址為0x000000076ab95cb8

    1
    2
    3
    4
    5
    6
    7
    8
    9
    "thread-test-b" #11 prio=5 os_prio=31 tid=0x00007fab0190b800 nid=0x5903 runnable [0x0000700010249000]
    java.lang.Thread.State: RUNNABLE
    at com.jasongj.demo.TestJstack.lambda$1(TestJstack.java:27)
    - locked <0x000000076ab95cb8> (a java.lang.Double)
    at com.jasongj.demo.TestJstack$$Lambda$2/1406718218.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

    Locked ownable synchronizers:
    - None

    Java中的鎖

    重入鎖

    Java中的重入鎖(即ReentrantLock)與Java內置鎖一樣,是一種排它鎖。使用synchronized的地方一定可以用ReentrantLock代替。

    重入鎖需要顯示請求獲取鎖,并顯示釋放鎖。為了避免獲得鎖后,沒有釋放鎖,而造成其它線程無法獲得鎖而造成死鎖,一般建議將釋放鎖操作放在finally塊里,如下所示。

    1
    2
    3
    4
    5
    6
    try{
    renentrantLock.lock();
    // 用戶操作
    } finally {
    renentrantLock.unlock();
    }

    如果重入鎖已經被其它線程持有,則當前線程的lock操作會被阻塞。除了lock()方法之外,重入鎖(或者說鎖接口)還提供了其它獲取鎖的方法以實現不同的效果。

    • lockInterruptibly() 該方法嘗試獲取鎖,若獲取成功立即返回;若獲取不成功則阻塞等待。與lock方法不同的是,在阻塞期間,如果當前線程被打斷(interrupt)則該方法拋出InterruptedException。該方法提供了一種解除死鎖的途徑。
    • tryLock() 該方法試圖獲取鎖,若該鎖當前可用,則該方法立即獲得鎖并立即返回true;若鎖當前不可用,則立即返回false。該方法不會阻塞,并提供給用戶對于成功獲利鎖與獲取鎖失敗進行不同操作的可能性。
    • tryLock(long time, TimeUnit unit) 該方法試圖獲得鎖,若該鎖當前可用,則立即獲得鎖并立即返回true。若鎖當前不可用,則等待相應的時間(由該方法的兩個參數決定):1)若該時間內鎖可用,則獲得鎖,并返回true;2)若等待期間當前線程被打斷,則拋出InterruptedException;3)若等待時間結束仍未獲得鎖,則返回false。

    重入鎖可定義為公平鎖或非公平鎖,默認實現為非公平鎖。

    • 公平鎖是指多個線程獲取鎖被阻塞的情況下,鎖變為可用時,最新申請鎖的線程獲得鎖。可通過在重入鎖(RenentrantLock)的構造方法中傳入true構建公平鎖,如Lock lock = new RenentrantLock(true)
    • 非公平鎖是指多個線程等待鎖的情況下,鎖變為可用狀態時,哪個線程獲得鎖是隨機的。synchonized相當于非公平鎖。可通過在重入鎖的構造方法中傳入false或者使用無參構造方法構建非公平鎖。

    使用jstack dump線程棧時,可查看到獲取到或正在等待的鎖對象,獲取到該鎖的線程會在Locked ownable synchronizers處顯示該鎖的對象類型及內存地址。在下例中,從Locked ownable synchronizers部分可看到,線程thread-test-e獲取到公平重入鎖,且該鎖對象的內存地址為0x000000076ae3d708

    1
    2
    3
    4
    5
    6
    7
    8
    "thread-test-e" #17 prio=5 os_prio=31 tid=0x00007fefaa0b6800 nid=0x6403 runnable [0x0000700002939000]
    java.lang.Thread.State: RUNNABLE
    at com.jasongj.demo.TestJstack.lambda$4(TestJstack.java:64)
    at com.jasongj.demo.TestJstack$$Lambda$5/466002798.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

    Locked ownable synchronizers:
    - <0x000000076af86810> (a java.util.concurrent.locks.ReentrantLock$FairSync)

    而線程thread-test-f由于未獲取到鎖,而處于WAITING(parking)狀態,且它等待的鎖正是上文線程thread-test-e獲取的鎖(內存地址0x000000076af86810

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    "thread-test-f" #18 prio=5 os_prio=31 tid=0x00007fefaa9b2800 nid=0x6603 waiting on condition [0x0000700002a3c000]
    java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <0x000000076af86810> (a java.util.concurrent.locks.ReentrantLock$FairSync)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
    at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:224)
    at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
    at com.jasongj.demo.TestJstack.lambda$5(TestJstack.java:69)
    at com.jasongj.demo.TestJstack$$Lambda$6/33524623.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
    Locked ownable synchronizers:
    - None

    讀寫鎖

    如上文《Java進階(二)當我們說線程安全時,到底在說什么》所述,鎖可以保證原子性和可見性。而原子性更多是針對寫操作而言。對于讀多寫少的場景,一個讀操作無須阻塞其它讀操作,只需要保證讀和寫或者寫與寫不同時發生即可。此時,如果使用重入鎖(即排它鎖),對性能影響較大。Java中的讀寫鎖(ReadWriteLock)就是為這種讀多寫少的場景而創造的。

    實際上,ReadWriteLock接口并非繼承自Lock接口,ReentrantReadWriteLock也只實現了ReadWriteLock接口而未實現Lock接口。ReadLock和WriteLock,是ReentrantReadWriteLock類的靜態內部類,它們實現了Lock接口。

    一個ReentrantReadWriteLock實例包含一個ReentrantReadWriteLock.ReadLock實例和一個ReentrantReadWriteLock.WriteLock實例。通過readLock()writeLock()方法可分別獲得讀鎖實例和寫鎖實例,并通過Lock接口提供的獲取鎖方法獲得對應的鎖。

    讀寫鎖的鎖定規則如下:

    • 獲得讀鎖后,其它線程可獲得讀鎖而不能獲取寫鎖
    • 獲得寫鎖后,其它線程既不能獲得讀鎖也不能獲得寫鎖
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    package com.test.thread;

    import java.util.Date;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReadWriteLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;

    public class ReadWriteLockDemo {

    public static void main(String[] args) {
    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    new Thread(() -> {
    readWriteLock.readLock().lock();
    try {
    System.out.println(new Date() + "\tThread 1 started with read lock");
    try {
    Thread.sleep(2000);
    } catch (Exception ex) {
    }
    System.out.println(new Date() + "\tThread 1 ended");
    } finally {
    readWriteLock.readLock().unlock();
    }
    }).start();

    new Thread(() -> {
    readWriteLock.readLock().lock();
    try {
    System.out.println(new Date() + "\tThread 2 started with read lock");
    try {
    Thread.sleep(2000);
    } catch (Exception ex) {
    }
    System.out.println(new Date() + "\tThread 2 ended");
    } finally {
    readWriteLock.readLock().unlock();
    }
    }).start();

    new Thread(() -> {
    Lock lock = readWriteLock.writeLock();
    lock.lock();
    try {
    System.out.println(new Date() + "\tThread 3 started with write lock");
    try {
    Thread.sleep(2000);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    System.out.println(new Date() + "\tThread 3 ended");
    } finally {
    lock.unlock();
    }
    }).start();
    }
    }

    執行結果如下

    1
    2
    3
    4
    5
    6
    Sat Jun 18 21:33:46 CST 2016  Thread 1 started with read lock
    Sat Jun 18 21:33:46 CST 2016 Thread 2 started with read lock
    Sat Jun 18 21:33:48 CST 2016 Thread 2 ended
    Sat Jun 18 21:33:48 CST 2016 Thread 1 ended
    Sat Jun 18 21:33:48 CST 2016 Thread 3 started with write lock
    Sat Jun 18 21:33:50 CST 2016 Thread 3 ended

    從上面的執行結果可見,thread 1和thread 2都只需獲得讀鎖,因此它們可以并行執行。而thread 3因為需要獲取寫鎖,必須等到thread 1和thread 2釋放鎖后才能獲得鎖。

    條件鎖

    條件鎖只是一個幫助用戶理解的概念,實際上并沒有條件鎖這種鎖。對于每個重入鎖,都可以通過newCondition()方法綁定若干個條件對象。

    條件對象提供以下方法以實現不同的等待語義

    • await() 調用該方法的前提是,當前線程已經成功獲得與該條件對象綁定的重入鎖,否則調用該方法時會拋出IllegalMonitorStateException。調用該方法外,當前線程會釋放當前已經獲得的鎖(這一點與上文講述的Java內置鎖的wait方法一致),并且等待其它線程調用該條件對象的signal()或者signalAll()方法(這一點與Java內置鎖wait后等待notify()notifyAll()很像)。或者在等待期間,當前線程被打斷,則wait()方法會拋出InterruptedException并清除當前線程的打斷狀態。
    • await(long time, TimeUnit unit) 適用條件和行為與await()基本一致,唯一不同點在于,指定時間之內沒有收到signal()signalALL()信號或者線程中斷時該方法會返回false;其它情況返回true。
    • awaitNanos(long nanosTimeout) 調用該方法的前提是,當前線程已經成功獲得與該條件對象綁定的重入鎖,否則調用該方法時會拋出IllegalMonitorStateExceptionnanosTimeout指定該方法等待信號的的最大時間(單位為納秒)。若指定時間內收到signal()signalALL()則返回nanosTimeout減去已經等待的時間;若指定時間內有其它線程中斷該線程,則拋出InterruptedException并清除當前線程的打斷狀態;若指定時間內未收到通知,則返回0或負數。
    • awaitUninterruptibly() 調用該方法的前提是,當前線程已經成功獲得與該條件對象綁定的重入鎖,否則調用該方法時會拋出IllegalMonitorStateException。調用該方法后,結束等待的唯一方法是其它線程調用該條件對象的signal()signalALL()方法。等待過程中如果當前線程被中斷,該方法仍然會繼續等待,同時保留該線程的中斷狀態。
    • awaitUntil(Date deadline) 適用條件與行為與awaitNanos(long nanosTimeout)完全一樣,唯一不同點在于它不是等待指定時間,而是等待由參數指定的某一時刻。

    調用條件等待的注意事項

    • 調用上述任意條件等待方法的前提都是當前線程已經獲得與該條件對象對應的重入鎖。
    • 調用條件等待后,當前線程讓出CPU資源。
    • 上述等待方法結束后,方法返回的前提是它能重新獲得與該條件對象對應的重入鎖。如果無法獲得鎖,仍然會繼續等待。這也是awaitNanos(long nanosTimeout)可能會返回負值的原因。
    • 一旦條件等待方法返回,則當前線程肯定已經獲得了對應的重入鎖。
    • 重入鎖可以創建若干個條件對象,signal()signalAll()方法只能喚醒相同條件對象的等待。
    • 一個重入鎖上可以生成多個條件變量,不同線程可以等待不同的條件,從而實現更加細粒度的的線程間通信。

    signal()signalAll()

    • signal() 若有一個或若干個線程在等待該條件變量,則該方法會喚醒其中的一個(具體哪一個,無法預測)。調用該方法的前提是當前線程持有該條件變量對應的鎖,否則拋出IllegalMonitorStateException
    • signalALL() 若有一個或若干個線程在等待該條件變量,則該方法會喚醒所有等待。調用該方法的前提是當前線程持有該條件變量對應的鎖,否則拋出IllegalMonitorStateException
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package com.test.thread;

    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;

    public class ConditionTest {

    public static void main(String[] args) throws InterruptedException {
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    new Thread(() -> {
    lock.lock();
    try {
    System.out.println(new Date() + "\tThread 1 is waiting");
    try {
    long waitTime = condition.awaitNanos(TimeUnit.SECONDS.toNanos(2));
    System.out.println(new Date() + "\tThread 1 remaining time " + waitTime);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    System.out.println(new Date() + "\tThread 1 is waken up");
    } finally {
    lock.unlock();
    }
    }).start();

    new Thread(() -> {
    lock.lock();
    try{
    System.out.println(new Date() + "\tThread 2 is running");
    try {
    Thread.sleep(4000);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    condition.signal();
    System.out.println(new Date() + "\tThread 2 ended");
    } finally {
    lock.unlock();
    }
    }).start();
    }
    }

    執行結果如下

    1
    2
    3
    4
    5
    Sun Jun 19 15:59:09 CST 2016  Thread 1 is waiting
    Sun Jun 19 15:59:09 CST 2016 Thread 2 is running
    Sun Jun 19 15:59:13 CST 2016 Thread 2 ended
    Sun Jun 19 15:59:13 CST 2016 Thread 1 remaining time -2003467560
    Sun Jun 19 15:59:13 CST 2016 Thread 1 is waken up

    從執行結果可以看出,雖然thread 2一開始就調用了signal()方法去喚醒thread 1,但是因為thread 2在4秒鐘后才釋放鎖,也即thread 1在4秒后才獲得鎖,所以thread 1的await方法在4秒鐘后才返回,并且返回負值。

    信號量Semaphore

    信號量維護一個許可集,可通過acquire()獲取許可(若無可用許可則阻塞),通過release()釋放許可,從而可能喚醒一個阻塞等待許可的線程。

    與互斥鎖類似,信號量限制了同一時間訪問臨界資源的線程的個數,并且信號量也分公平信號量與非公平信號量。而不同的是,互斥鎖保證同一時間只會有一個線程訪問臨界資源,而信號量可以允許同一時間多個線程訪問特定資源。所以信號量并不能保證原子性。

    信號量的一個典型使用場景是限制系統訪問量。每個請求進來后,處理之前都通過acquire獲取許可,若獲取許可成功則處理該請求,若獲取失敗則等待處理或者直接不處理該請求。

    信號量的使用方法

    • acquire(int permits) 申請permits(必須為非負數)個許可,若獲取成功,則該方法返回并且當前可用許可數減permits;若當前可用許可數少于permits指定的個數,則繼續等待可用許可數大于等于permits;若等待過程中當前線程被中斷,則拋出InterruptedException
    • acquire() 等價于acquire(1)
    • acquireUninterruptibly(int permits) 申請permits(必須為非負數)個許可,若獲取成功,則該方法返回并且當前可用許可數減permits;若當前許可數少于permits,則繼續等待可用許可數大于等于permits;若等待過程中當前線程被中斷,繼續等待可用許可數大于等于permits,并且獲取成功后設置線程中斷狀態。
    • acquireUninterruptibly() 等價于acquireUninterruptibly(1)
    • drainPermits() 獲取所有可用許可,并返回獲取到的許可個數,該方法不阻塞。
    • tryAcquire(int permits) 嘗試獲取permits個可用許可,如果當前許可個數大于等于permits,則返回true并且可用許可數減permits;否則返回false并且可用許可數不變。
    • tryAcquire() 等價于tryAcquire(1)
    • tryAcquire(int permits, long timeout, TimeUnit unit) 嘗試獲取permits(必須為非負數)個許可,若在指定時間內獲取成功則返回true并且可用許可數減permits;若指定時間內當前線程被中斷,則拋出InterruptedException;若指定時間內可用許可數均小于permits,則返回false。
    • tryAcquire(long timeout, TimeUnit unit) 等價于tryAcquire(1, long timeout, TimeUnit unit)*
    • release(int permits) 釋放permits個許可,該方法不阻塞并且某線程調用release方法前并不需要先調用acquire方法。
    • release() 等價于release(1)

    注意:與wait/notify和await/signal不同,acquire/release完全與鎖無關,因此acquire等待過程中,可用許可滿足要求時acquire可立即返回,而不用像鎖的wait和條件變量的await那樣重新獲取鎖才能返回。或者可以理解成,只要可用許可滿足需求,就已經獲得了鎖。

    Java進階系列

    郭俊 Jason wechat
    歡迎關注作者微信公眾號【大數據架構】
    您的贊賞將支持作者繼續原創分享
    速赢彩app www.3200.com | 8742t.com | www.188721.com | www.88ac.com | www.77801f.com | www.8883066.com | www.7779855.com | www.4938t.com | 3024p.com | 9994cc16.com | 39991122.com | 80892pp.com | 7605j.com | www.729937.com | www.33588g.com | www.68365u.com | www.89677x.com | www.cp55569.com | www.063801v.com | www.ag3131g.com | 2546v.com | 3522gg.com | acp0006.com | mgm3242s.com | 89777o.com | www.021067.com | www.938537.com | www.0270u.com | www.js89j.vip | www.917090.com | www.gdnmi.com | www.836690.com | www.32666h.com | www.63877e.com | 500000476.com | 33ww8331.com | xpj677r.com | uc03.cc | n7454.com | 4638877.com | www.338082.com | www.hm3988.com | www.99638d.com | www.491407.com | www.46630.info | www.55545i.com | www.kjlhc.cc | www.d526688.com | www.bmw810.net | www.495655.com | www.xpj99897.com | www.6767msc.com | www.95679.com | www.zun371.com | www.888476.com | yin8888.tv | n47479.com | 83377l.com | x33f.vip | 2247gg.com | 3434hhh.com | bj299.com | wnsr8824.com | 3438w.com | HGW3300.com | 6269pp.com | 7439.com | hg33382.com | 97799o.com | hb8331.com | 35252k.com | 112a.net | 5429d.com | 3456789.net | 1168t.com | www.63260.com | www.7720p.com | www.0636b.com | www.vns388.com | www.hg6969.net | www.8js189.com | www.86611u.com | 1592m.com | www.1116xj.com | www.a3a666.cc | www.vns1209.com | www.88807p.com | www.14900b.com | www.601066.com | www.2544h5.com | www.8082.am | www.6889793.com | www.80065y.com | www.77ckb.com | www.544646.com | 6766hh.com | 8892958.com | lehu998.com | 33112007.com | www.a3a999.cc | www.js7888.co | www.52.cc | www.06386677.com | www.81678g.com | www.c1360.com | www.168767.com | 97799g.com | 69111w.com | df8bet.com | www.81866r.com | www.hg8997.com | www.751pk10.com | www.38775tt.com | www.78700m.com | www.745955.com | 809h50.com | 3467b.am | g2383.com | www.pjbet777.com | www.pj8333.com | www.4521k.com | www.80065t.com | www.675630.com | vns8g.com | tt44442.com | 168cp-t.com | www.88993d.com | www.938w.cc | www.5350.vip | www.773925.com | vs9777.com | 0015.com | www.hg9951.com | www.79111.com | www.w3410.com | www.01500w.com | www.287916.com | 22227893.com | 57877.com | www.8694i.com | www.cp0028.com | www.h6630.com | www.810531.com | 29918i.com | 3405f.com | 1408f.com | www.822822.com | www.2848aaa.com | www.595hc.com | 8036a.com | 93936i.com | 528866.com | www.2997701.com | www.883399e.com | www.lczg5.com | 3122vip7.com | 4647p.com | www.2090977.com | www.697002.com | www.508048.com | www.178615.com | 8569899.com | yh888e.com | www.h1765.com | www.68993227.com | www.340680.com | 0209.com | www.7669d.net | www.0636777.com | www.32123l.com | vv968.com | 59199.com | www.205611.com | www.00772r.com | www.5522i.cc | 7779e.cc | pj12234.com | www.81776.com | www.07770.comm | www.622989.com | lehu80.com | 47749c.com | www.28mscc.com | www.89894p.com | pjbet333.com | 61322288.com | www.23336.com | www.1206c.com | www.986399.com | 93910.com | www.234374.com | www.sha0033.com | www.858899a.com | 3678kkk.com | 923560.com | www.hf9012.com | www.50052m.com | vs9777.com | fll8.com | www.2945f.com | www.6832w.com | 3950h.com | 8036ll.com | www.9149.com | www.hx1163.com | 2077bb.com | www.210zr.com | www.bwinyz40.com | www.560926.com | 08778g.com | www.wn99ww.com | www.7681006.com | www.029509.com | 88993v.com | www.423886.com | www.yh66766.com | js14g.com | 2373x.com | www.99113x.com | www.929709.com | 30172266.com | www.8sands.com | www.66136000.com | js75.com | www.333222m.com | www.89386.com | www.77801i.com | bet28s.com | www.lu8810.com | www.fen00.com | 4809x.com | www.6662558.com | www.5856862.com | xpj66498.com | www.ds22222.com | www.9895a.com | www.140018.com | 76886k.com | www.c63.com | www.701514.com | 3846ff.com | www.31441.com | www.3552q.com | 7249d.com | www.hg246.com | www.50054h.com | u32365.com | www.o30226.com | www.ya2019d.com | 3132121.com | www.8124d.com | www.25683.com | ambjlc.com | www.4111.cc | www.954321x.com | b62d.com | www.288msc.com | www.7714a.com | y68com | www.330099y.com | www.55228b.com | 22772007.com | www.991866.com | www.lczg1.com | 4136g.com | www.hemi168.com | www.576530.com | 4647o.com | www.4058aa.com | 15856y.com | aobo11111.com | www.7240q.com | www.234894.com | www.66073.cc | afcp10.com | www.hjcp111.com | www.550377.com | 87965mm.com | www.26123aa.com | 89892pp.com | 63963333.com | www.168749.com | 2381q.com | www.hg8205.com | www.636550.com | 31784444.com | www.5551883.com | 1294j.com | www.184589.com | www.900248.com | 26668.com | www.33678tt.com | zhcp100.com | www.w8867.com | www.95.la | h51335.net | www.601709.com | 3568m.com | www.699888.com | www.cp3.xyz | 6261rr.com | www.32031r.com | 5309e.com | www.13201f.com | www.606694.com | 2247t.com | www.0096788.com | 0616.com | www.04567t.com | 27878mm.com | www.32666k.com | www.966082.com | 22556i.com | www.mgm777c.com | bwinqqq.com | www.8967q.com | 3938hh.com | www.wl6666.cc | www.785803.com | 168cp-x.com | www.sx139975.com | nn3405.com | www.2021d.com | 9479vip.com | www.8zhizun.com | www.43818u.com | www.995h.net | www.689244.com | long677.com | www.33598y.com | 444b9.com | www.72002.com | gui474.com | www.00778r.com | 11004002.com | www.89599u.com | 44gg8332.com | www.55526w.com | 8901y.com | www.ytgj444.com | www.099192.com | www.7025c.com | www.602630.com | www.85770l.com | www.c1336.com | 74668.com | www.1368y.cc | 1484vip2.com | www.csgc5.com | 51200jj.com | www.003ac.com | 5456o.com | www.84gcw.com | 55115156.com | www.c6356.com | 1119193.com | www.ybao0.com | 7736w.com | www.gczj3.com | 45460009.com | www.915231.com | www.4446333.com | www.918361.com | www.vns388.com | www.685548.com | www.3657z.com | www.567106.com | www.yh5579.com | www.351957.com | www.7111f.com | www.89894e.com | www.tyc55.com | 88992007.com | www.3171110.com | 61789l.com | www.50999q.com | 1483b.com | www.79095e.com | 566777u.com | www.68993239.com | 28485000.com | www.if57.com | www.boma0150.com | www.105636.com | www.90585.com | 81511n.com | www.yc7777.com | 168b1.com | www.634505.com | 4018ss.com | www.50052hh.com | 3258z.com | www.hf0168.com | www.benz4433s.com | www.137916.com | www.22076.com | vic9308.com | www.e7764.com | 666x2.com | www.25688e.com | 5003ff.com | www.630155.com | www.xpj04666.com | 11422.com | www.7225t.com | 4541i.com | www.39957c.com | www.666k8.com | www.327836.com | www.31955.com | x8519.com | www.xpj8869.com | y68147.com | www.c1968.com | www.wns10.com | b987.xyz | www.1108598.com | 3522g.vip | www.32czj.com | www.09569q.com | 272yy.net | www.bwinyz37.com | 1253-2.com | www.ya350.com | www.vns9603.com | xinli18sport.com | www.b83377.com | yhanhui.vip | www.807835.com | www.08057777.com | jixian678.com | www.8s44.com | www.00217g.com | vdisk.cn | www.38138t.com | qq365i.com | www.hy6935.com | www.9558jsc.com | feicai0991.com | www.42842816.com | www.5550105.com | 54443v.com | www.bb673888.com | 88851e.com | www.564676.com | www.868msc.com | lc99r.com | www.ztc7.com | www.736suncity.com | 78118833.com | www.8499l.com | www.284444.me | 156050.com | www.3978i.com | www.www-866766.com | 123456hh.cc | www.n63568.com | www.557zr.com | 500000513.com | www.69567i.com | xpj588.com | js75ff.com | www.65045.com | 33v333.com | 0208065.com | www.vns000.me | www.hg08678.com | 86mv.com | www.0003sc.com | www.9785555.com | zyrb.com.cn | www.173383.com | www.22401.com | l3405.com | www.43818.com | www.31268.com | s58955.com | www.c5236.com | www.hd779.com | 33ff8332.com | www.303483.com | www.9570118.com | 88113885.com | 888991155.com | www.8084.com | www.954555.com | 2542244.com | www.933345.com | www.22cp163.com | 55aa8332.com | 8901z.com | www.906777.com | www.77537n.com | 3y.com | www.891085.com | www.b3065.com | feicai0354.com | 55970.net | www.7714x.com | www.299755k.com | 888991188.com | www.444086.com | www.2021h.com | www.1019995.com | 3679qq.com | www.sjgc5.com | www.534359.com | 0747z.com | 3522tt.cc | www.26878b.com | www.114888.com | 7439.com | 58222bb.com | www.32123r.com | www.3334msc.com | 3169z.com | 5856u.com | www.5578w.com | www.c44vv.com | 3131a.cc | 60952222.com | www.811675.com | www.45598y.com | www.js46123.com | 07100.com | www.208990.com | www.5zz66.com | www.5816ee.com | 56787xx.com | 272f.net | www.41518.com | www.668cp33.com | 00014066.com | 3467z.cc | www.500770.com | www.306531.com | www.h7733.com | 91019g.com | 61652u.com | www.997345.com | www.377666u.com | www.7025d.com | 4182x.com | 444365ggg.com | www.5441m.com | www.16181d.com | www.hg9946.com | 4022kk.com | feicai0379.com | www.6832o.com | www.7334a.com | www.dy6611.com | 641768.com | yk801.com | www.878zf.com | www.1754m.com | www.2846d.com | 2864x.com | xj001c.com | www.355241.com | www.22211d.com | www.184897.com | www.xhtd02.com | hggjtg19.com | 1458n.com | www.690802.com | www.833296.com | www.3122d.com | www.76060c.com | 3950r.com | c558.cc | www.914905.com | www.99094i.com | www.352233.com | www.hg0633.com | 61653e.com | ab0034.com | www.500106.com | www.91233s.com | www.4bet005.com | www.16297788.com | v99932.com | 3990055.com | 6641o.com | www.649677.com | www.yh66988.com | www.00772d.com | www.hhgg24.com | 7075365.com | 78808n.com | blhvip9.com | www.218292.com | www.78700.com | www.js89i.vip | www.kxm06.com | www.yy883.com | mf705.com | 01234r.com | ra333333.com | www.376357.com | www.33112s.com | www.490806.com | www.56655i.com | www.0011152.com | 22296uu.com | 82365j.com | 9895e.com | 23800y.com | www.739233.com | www.81678k.com | www.4546100.com | www.l9478.com | www.1389u.com | 6403i.com | 45637k.com | 69619.com | 6146k.com | www.65707a.com | www.074wy.com | www.js9999.bet | www.n80288.com | www.a98886.com | www.66930055.com | www.hg55768.com | 97799o.com | 4465s.com | 00048r.com | 3156iii.com | ylzz1113.com | www.515233.com | www.00ckb.com | www.6653i.com | www.56011n.com | www.11tyc.com | www.48282222.com | www.yinhe899.com | www.19888bet.com | www.444jsc.com | kk56988.com | 28288y.com | 4018x.com | tz1666.com | wns981.com | 1483y.com | 6668009.com | www.371956.com | www.938751.com | www.5086p.com | www.86267d.com | www.81608r.com | www.47506j.com | www.pj5958.com | www.303800.com | www.hg6888.com | www.yh77899.com | www.tyc9907.com | 3178u.com | 234903.com | 8888c9.com | 5651g.com | 1479.cc | 66005002.com | www.81233o.com | www.681770.com | www.w796.com | www.3126r.com | www.60123r.com | www.9989582.com | www.o63568.com | www.42070013.com | www.32031f.com | www.1851138.com | www.bet91480.com | www.4212t.com | www.163771.com | www.1389oo.com | www.896suncity.com | www.hg920.com | www.5882323.com | www.hg8447.com | www.7392022.com | www.vns6985.com | www.8694y.com | www.j8877.com | www.pjbet444.com | www.alpk88.com | www.06068006.com | www.825909.com | www.2211sb.com | www.70msc.com | www.142336.com | www.0621gg.com | www.223228.com | www.tyc1802.com | www.t45688.com | www.354488.com | www.tm012.com | www.088878.com | www.suncity758.com | www.557244.com | www.45682.com | www.xcw988.com | www.8313w.com | www.x80288.com | www.383335.com | www.1434q.com | www.x666678.com | www.0194006.com | www.66332s.com | www.3933t.cc | www.929115.com | www.606614.com | www.186357.com | vv38648.cc | qp168.shsxiwl.cn | 0057a.com | 2146y.com | 2214jj.com | 01885e.com | 37570p.com | www.123456pk.com | www.4107p.com | www.6834j.com | www.087hg.com | www.45682.com | www.45598s.com | www.4809p.com | www.173383.com | www.3479u.com | www.771246.com | www.160592.com | 44488a.com | 6946622.com | 30177722.com | 33560055.com | 57157g.com | www.668811.com | www.367888a.com | www.yingle55.com | www.4058mm.com | www.4833043.com | www.43818h.com | www.955381.com | www.le33.cc | 377633.com | 111hg88.com | lehu1013.com | 228888o.com | www.77759333.com | www.777348.com | www.19019l.com | www.vns44.me | www.50732n.com | www.782478.com | 3556vip7.com | 2021www.com | 8037iii.com | 3379aa.com | www.849558.com | www.056005.com | www.hg6668.ph | www.28891h.com | www.rrle6.com | www.101125.com | 2805d.com | z21148.com | 3122ll.com | www.d30226.com | www.cr811.com | www.7036g.com | www.55228b.com | www.577972.com | 214sj.com | 01234y.com | 64111u.com | www.2846n.com | www.p456x.com | www.35996.com | www.78wcp.com | www.36788u.com | 6601.com | 00cc8331.com | www.059778.com | www.2y935.com | www.i63568.com | www.sygj1.com | blr2033.com | 1669w.com | 6220p.com | www.6403jj.com | www.5446hh.com | www.176.ag | www.511039.com | 1007811.com | hg88000.com | www.30765.com | www.a9151e.com | www.1117.com | www.793990.com | 6220x.com | dw777.net | www.6535.com | www.27363j.com | www.37377c.com | www.652162.com | s86811.com | 8977aa.com | www.777444g.com | www.5555yh.com | www.80075l.com | www.178257.com | 53059.vip | 1318345.com | www.29962.com | www.pj55716.com | www.978366.com | 9895u.com | 6261q.com | www.7111t.com | www.9570111.com | www.3126a.com | zhcp10.com | 67890ccc.com | www.hg1098.com | www.9737mm.com | www.9356q.com | 7744aaa.com | 3846ll.com | www.00829k.com | www.9170187.com | www.556zf.com | 2776b.com | t77304.com | www.4938w.com | www.r1432.com | www.c1395.com | 61329966.com | 500000996.com | www.32666l.com | www.222999qipai.com | www.452166.com | 727720.com | www.pj57777.com | www.yk222d.com | www.23022.cc | mm67890.com | 71017g.com | www.4965a.com | www.h3658.com | www.81233v.com | 6664553.com | www.nbe666.com | www.vnsr928.com | www.607315.com | 55899t.com | www.ks299.net | www.969709.com | www.ch8678.com | 3mgmiii.com | 18775c.com | www.88166b.com | www.hczx4.com | hd81366.com | www.68666v.com | www.5099ww.com | www.hm7388.com | 3189x.com | www.hg9016.com | www.6880mm.com | www.915399.com | 12274477.com | www.365815c.com | www.3846bb.com | www.196705.com | 36404400.com | www.5816ee.com | www.6613655.com | 7744ooo.com | 6245h.com | www.dfh200.com | www.czg2.com | 00555004.com | www.bet8300.com | www.89386e.com | www.136031.com | 80850hh.com | www.xg189.net | www.50024c.com | ppp4165.com | www.99999jsc.com | www.652513.com | www.391438.com | 4152d.com | www.08111.com | www.51331m.com | 777hg.com | www.ao699.com | www.60886o.com | www.022197.com | 53015.com | www.07.bet | www.799666a.com | 69444455.com | www.yy085.com | www.99788s.com | 99775156.com | www.ao699.com | www.xpj8558.com | feicai0511.com | 71245.com | www.4078k.com | www.375967.com | 8449mm.com | www.jxcp2222.com | www.891477.com | 3807c.com | www.81321a.com | www.022y.cc | www5682.com | www.349459.com | www.8667s.com | 8438000.com | www.58505.com | www.50026w.com | js6780.com | www.5303v.com | www.34788r.com | 8381ii.com | www.6699mmmm.com | www.8667m.com | 99909k.com | www.pj43.com | www.qbwc1.com | 4577.com | www.1388707.com | www.715585.com | 85698q.com | www.677256.com | www.303853.com | 2373d.com | www.91779n.com | 1136.cc | www.789pj.com | www.h6620.com | 11bwi.com | www.7111b.com | www.00840l.com | 53206622.com | www.001547.com | www.896165.com | 00yy8332.com | www.16181d.com | wzyxwz.org | www.358msc.com | www.0967.bet | 61652q.com | www.vns2018.com | www.hm8222.com | 496.es | www.5981r.com | 077077.com | www.28758u.com | www.0270l.com | 7134455.com | www.12706.com | www.431506.com | 56988r.com | www.zsjzxyy.com | 3304w.com | www.675923.com | www.915044.com | wnsr8816.com | www.40288h.com | x7267.com | www.4270pp.com | www.hm5522.com | 2127bb.com | www.565636.com | 52688u.com | www.11447712.com | www.821260.com | 2649c.com | www.rrqp333.com | 1434p.com | www.mgdc42.com | www.249770.com | www.86611g.com | www.96386r.com | 3467g.am | www.bet91489.com | 4048ccc.com | www.yh8352.com | www.911283.com | by9911.com | www.ihg5566.com | 1919394.com | www.170507.com | 78666c.com | www.0860q.com | www.901591.com | f35151.com | www.9238777.com | 6633477.com | www.7334b.com | 36408811.com | www.24suncity.com | www.501342.com | www.ab151.com | www.78995.com | df8g.com | www.676711.com | 78118833.com | www.4963nn.com | 3559cccc.com | www.hg6556.com | www.268177.com | www.087087.com | www.cpkk7.com | 777777070.com | www.8d222.com | 4289t.com | www.v1068.com | 6668449.com | www.sscb22.cc | 3007811.com | www.759696.com | youfa99.vip | www.cs505.com | www.235187.com | www.76543z.com | www.769289.com | www.gh0092.com | www.005ac.com | 35252y.com | www.41518k.com | nn500h.com | www.588904.com | kbf6688.com | www.h6640.com | bet727.com | www.63800a.com | mg4151.vip | www.38775bb.com | 91019b.com | www.30350w.com | vv879404.com | www.115527n.com | 7249q.com | www.80188w.com | 6519822.com | www.bwinyz43.com | 3176688.com | www.696668.com | hg7033.org | www.781617.com | 11422.vip | www.1030360.com | 00774ll.com | www.6687g.com | 73055w.com | www.882229.com | 3950u.com | www.7240p.com | 7811hh.com | www.6939n.com | 80368ee.com | www.81520x.com | 2247gg.com | www.8499d.com | yl77yl33.com | www.984688.com | www.76060s.com | www.549077.com | www.32666m.com | 29918k.com | www.786666.com | 4123ss.com | www.8473w.com | blr969.com | www.m63568.com |