• <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 P35y.com | 9895l.com | www.138908.com | 66300vip39.com | www.j69096.com | 815077.com | www.800795.com | ag0101g.com | www.3566kk.com | 3131394.com | www.222067.com | vns300.net | www.808791.com | www.5559193.com | nck5273.com | www.19019h.com | l2146.com | www.w94600.com | 654.so | www.c6089.com | www.85770n.com | 8449ww.com | www.88470.com | 4880i.com | www.1119yl.com | laok1188.com | www.345576.com | www.0077jj.com | 4255q.com | www.js79903.com | aa5002.com | www.12czj.com | www.hjdc2001.vip | z3144.com | www.4996zz.com | 9287999.com | www.fcsj.com | www.xh538.com | 88850kk.com | www.86339r.com | 4023i.com | www.561518.com | www.tai111.com | x67890.com | www.919654.com | www.e0686.com | 9895m.com | www.69567f.com | 2247x.com | www.183676.com | www.634599.com | 75991m.com | www.8039g.com | www.25288t.com | mw0621.com | www.js9090.com | www.wn0003.com | le888r.com | www.69567f.com | www.999qipai.com | www.7720n.com | 28824b.com | www.115527e.com | 11989h.com | 44ff8331.com | www.222335.com | www.ag9921.com | 4255pp.com | www.386123.com | www.bwin299.com | 8128522.com | www.105607.com | www.js520234.com | r88.com | www.fh7577.com | www.878msc.com | 9999hd.com | www.c6078.com | www.js7575.com | 51133p.com | www.338061.com | www.4058uu.com | 7935g.com | t388399.com | www.00xpj918.com | www.4107o.com | zhcpyy.com | www.c1360.com | www.91gwc.com | 443636.com | www.202340.com | www.vip9581.com | fc9596.com | 65560055.com | www.50026y.com | www.js0096.com | b33668.com | www.356686.com | www.88325m.com | www.6491d.com | 7736r.com | www.uucp300.com | www.445466.com | df8m.com | 4255sss.com | www.4058.com | www.xam88888.wang | ee7742.com | 5001v.com | www.p63568.com | www.h32126.cc | 4461z.com | www.338996.com | www.h94600.com | www.3643i.com | 80567g.com | www.6364h.com | 22299ii.com | www.78919d.com | www.v15542.com | 1339.com | www.068652.com | www.9989579.com | www.vns9958.com | 82509.com | www.150881.com | www.06660.com | www.8366uu.com | 8522llll.com | 20776688.com | www.986jc.com | www.4996ln.com | www.vns9998.com | 3568ww.com | ule8.vip | www.50026y.com | www.38345q.com | www.653504.com | 336.cc | www.202427.com | www.36582222.com | www.cp66611.com | www.1114xj.com | 38244l.com | 11452233.com | www.qucp7.com | www.282654.com | www.a2399.com | jindaye88.com | 5443e.com | www.852086.com | www.1678.cc | www.hg035.com | www.vns2533.com | lh66x.com | 66300vip50.com | www.5086k.com | www.9570116.com | www.vvv2205.com | 3258y.com | www444000.com | www.504277.com | www.967004.com | www.a81m.cc | www.7830e.com | yz222999.com | feicai0598.com | www.115669.com | www.83993k.com | www.531789.com | www.ttn88.com | 8957b.com | 15q15.net | www.094499.com | www.00czj.com | www.166490.com | www.000amdc.com | www.76543k.com | 6830z.com | 83138o.com | 9539g.com | www.yi628.com | www.36582222.com | www.5446w.com | www.jjjj22222.com | www.76543d.com | 5429q.com | 3967g.com | 87680p.com | www.552703.com | www.9478j.com | www.a3410.com | www.zzyl68.com | www.31567.ag | www.87879193.com | www.4759bb.com | 4488zzz.com | 1813vip.com | 5804e.com | 55ww8331.com | www.109075.com | www.588yc.com | www.3978y.com | www.xpj2.net | www.79095w.com | www.yf230.com | www.msc835.com | www.xpj3378.com | 2418004.com | 61323366.com | nn3405.com | 8448.com | bet99338.com | 2306i.com | P35zz.com | aa5443.com | www.52303u.com | www.595707.com | www.qucw1.com | www.50732l.com | www.v5995.com | www.1117795.com | www.00772r.com | www.51705.com | www.21511x.com | www.36225.com | www.pj0007.com | www.tyc8808.com | www.237015.com | 8905i.com | yhshandong.vip | 67890www.com | 3651462.com | 9339999t.com | 61325577.com | pj38pj38.com | 6245h.com | 4858xianlu.com | 63965555.com | 131b.net | o83377.com | js75c.com | www.00018js.com | 44449193.com | www.hg444.tw | www.pj8585.com | www.tm357.com | 1592q.com | 58802p.com | 883399.com | 2355o.com | feicai0830.com | 3136rr.com | 4997e.com | 365488.bet | 88p88.cc | 97618b.com | www.chun1983.com | www.ydb44.com | www.287720.com | www.swj68.com | www.kk4666.com | www.846239.com | www.2yhyh.com | www.79095m.com | www.38775cc.com | www.3691b.com | www.7714f.com | www.33997b.com | www.c6097.com | www.444059.com | www.36788i.com | 87680p.com | 30179900.com | vv01234.com | ojbk188.com | 2613u.com | www.8804js.com | www.55268qq.com | www.20161122.com | www.zgzcw.com | www.c668.cc | www.cn365u.com | www.p2894.com | www.aobo194.com | www.80065t.com | www.hx6626.com | www.577513.com | 20054466.com | 7334t.com | b1915b.com | hg2886.com | 6655734.com | www.21365ff.com | www.20976.com | www.k32939.com | www.869934.com | www.96386h.com | www.50054r.com | www.890752.com | www.371411.com | 6363dd.com | 80892q.com | 4022uu.com | 3544e.com | www.6686450.com | www.hqr66.com | www.986076.com | www.56011r.com | www.33068.cc | www.83033o.com | 1294u.com | 4541b.com | www.1869p.com | www.pj2598.com | www.80969b.com | www.038839.com | www.022f.cc | www.51515a.com | 8977ww.com | jinguanylc9.com | www.pjbet222.com | www.799666p.com | www.138.net | www.34788d.com | www.303471.com | 20773388.com | 6261nn.com | www.365168.cc | www.048888.com | www.h2894.com | www.3478a.cc | 2021b.com | 4255uuu.com | dd5443.com | www.122cpb.com | www.sands2008.com | www.35155r.com | 2602004.com | 19yl.com | 121ccc.com | www.wnsr5550.com | www.1764q.com | www.619939.com | 3238.com | kk38648.com | www.hg0092.com | www.45598d.com | www.50048.com | 923.com | 77606z.com | www.7720q.com | www.120509.com | www.68689x.com | www.397730.com | wnsrw.vip | 3009q.com | www.7168805.com | www.pjc55.vip | www.810877.com | 11989y.com | nn5443.com | www.195666.com | www.6133.com | www.636550.com | yy76669.com | v1647.com | www.js600000.com | www.27280.cc | cp088i.com | yhxjaomen.vip | www.as0005.com | www.8905c.com | www.580820.com | 1665hh.com | www.8js189.com | www.16181o.com | www.9205c.com | 2535b.com | 28758v.com | www.bai3355.com | www.78700b.com | 844.cc | pj911c.cc | www.858822.com | www.hg99957.com | www.41518o.com | 3482p.com | www.553525.com | www.m98478.com | www.129989.com | 884167.com | www.881389.com | www.32123z.com | 99151z.com | nn5443.com | www.dzj0110.com | www.730631.com | 9649j.com | www.09569r.com | www.h36524.com | 01885b.com | feicai0599.com | www.00773d.com | www.980205.com | 99699q.com | www.wn2098.com | www.3421h.com | 15856o.com | 26431818.com | www.99113j.com | www.ya278.com | 3863.com | www.xinhao77.com | www.5091v.com | ff555.com | www.hg7227.com | www.6482b.com | qq7742.com | feicai0830.com | www.7415h.com | www.270667.com | 2997778.com | www.bwinyz31.com | www.50080v.com | g77304.com | www.85857p.com | www.810523.com | ff7742.com | www.hg5526.com | www.61233y.com | 00442015.com | www.gd49.com | www.849129.com | 2757l.com | www.9737qq.me | www.083016.com | 8827ii.com | www.1851222.com | www.175568.com | 2805n.com | www.xpj8668.com | tyc.com | www.29886k.com | www.41518j.com | 8016aa.com | www.6666hs.com | www.592113.com | bali3377.com | www.js89v.vip | 6830h.com | www.9679a.com | www.5881.cc | 6220a.com | www.820032.com | 2013.co | www.589234.com | www.1117758.com | 1869.com | www.2833555.com | www.35155m.com | 4018hh.com | www.55070f.com | 55977.com | www.77ff940.com | www.ya130.com | 40033q.com | www.9925546.com | 20188d.com | www.77759333.com | www.flff6.com | 2078g.com | www.360cp.com | 2306.com | www.pj7668.com | www.799100.com | s2649.com | www.xpj8668.com | 8890823.com | www.3171114.com | amhj.cc | www.h7788m.com | www.99jti.com | f47479.com | www.5599msc.com | 55822.com | www.011116.com | 1665gg.com | www.z22365.com | www.7782r.com | 9737oo.me | www.88599.com | 2147006.com | www.55070y.com | 910277.com | www.335503.com | www.160886.com | www.056987.com | www.925771.com | xpj58058.com | www.01500w.com | 35442888.com | www.68365v.com | ww0332.com | www.555838.com | 28824p.com | www.pj88n.com | 48139.com | www.blr336.com | 44ff8332.com | www.11119u.com | www.77801o.com | www.n30226.com | www.558413.com | www.56733w.com | www.77210f.com | 6118q.com | www.63683.com | zb152.com | www.8499n.com | www.215566.com | www.550377.com | www.jnh663.com | 1429x.com | www.eb000.com | 22448332.com | www.1558666.com | csc998.com | www.06czj.com | www.8658822.com | www.021037.com | www.766577.com | ygl789.com | www.1813bb.com | 6146s.com | www.26299.com | www.85657a.com | www.229958.com | www.hgw675.com | w3298.com | www.7737ff.com | xpj78333.com | www.830523.com | www.hg66997.cc | 6168.cc | www.7708598.com | 5309789.com | www.022f.cc | www.7830t.com | 3136uu.com | www.hgw6668.com | xpj01000.com | www.082wy.com | www.p8867.com | 00773n.com | www.738055.com | 9694x.com | www.808731.com | www.33352055.com | 53262bb.com | www.sa606.com | tt4675.com | www.88065a.com | www.rgcp.vip | 520159407.com | www.fyyy8.com | www.vns00ss.com | 56811.com | www.28891j.com | www.un3355.com | pjbet333.com | www.06820p.com | uuu5682.com | www.510860.com | www.69896.com | 4288x.com | www.658zf.com | www.674suncity.com | hjdc988.com | www.7406.Co | www.87680x.com | 5005w.com | www.28891j.com | www.223456r.com | 6245w.com | www.77424.com | www.vnsr800.com | kn8pe8fq.pp.163.com | www.556350.com | www.pj25555.com | 1483f.com | www.1434o.com | www.hg0377.com | b2649.com | www.81608i.com | www.621155d.com | 3467n.am | www.81233z.com | www.xpj220.com | y2557.com | www.6dwj.com | www.59875.com | 17792222.com | www.282990.com | www.4996js.com | 1624.com | bosw8.com | www.55238g.com | www.qqqqkkkk.com | 3143.cc | www.aj3383.com | www.g9478.com | 8988o.com | 29918qq.com | www.833bbb.com | www.83hg.com | df8w.com | www.979673.com | www.1168t.com | zhcp43.com | yk802.com | www.bet73h.com | www.49981k.com | 55967d.com | www.236844.com | www.wns123b.com | www.bet3650614.com | hg12020.com | www.qilc3.com | www.79500r.com | 5589333.com | 1389mm.com | www.9149j.com | www.5966sss.com | 83377n.com | s00351.com | www.43818.cc | www.50000992.com | 7610.com | 713348.com | www.zfcp5.com | www.98888js.com | www.1869r.com | pj00aa.com | www.918361.com | www.c80288.com | www.lfg555.com | 4556v.com | www.97655e.com | www.4331o.com | www.286688.com | bbc51133.com | 696.net | www.hm2999.com | www.38138p.com | www.hg6642.com | 6099333.com | yun968.com | www.50788b.com | www.7779566.com | www.yh8004.com | aa116.net | www.78949v.com | www.6653d.com | www.6778msc.com | hggjtg15.com | yz01.cc | www.43131g.com | www.hg0088.so | www.q948q.com | www.hg9016.com | 463h8.com | 62105.com | www.9149e.com | www.5203666.com | www.b27229.com | 2234.cc | xhgx2.com | www.648689.com | www.7249n.com | www.hg7660.com | 9420d.com | 55967.com | 5446v.com | www.cp8006.cc | www.68365m.com | www.090051.com | www.lpj789.com | 32212l.com | 01234pp.com | www.799243.com | www.37377r.com | www.hr888vip.com | www.21365bb.com | m99345.am | 59963.la | 1389nn.com | www.sygj9.com | www.hg9209.com | www.88100.com | www.yh8214.com | h72227.com | 4255ee.com | bj499.com | www.c6770.com | www.48330e.com | www.58665b.com | www.7111x.com | 500000477.com | 35222xx.com | 7894q.com | www.298396.com | www.07163g.com | www.07679p.com | www.hg7760.com | www.a22365.com | 500000370.com | 4022aa.com | qq7819.com | www.063911.com | www.zch2.com | www.115107.com | www.1429g7.com | www.76775g.com | www.h7788n.com | 56988u.com | 7240p.com | 999hg.com | ee76669.com | www.58gc8.com | www.66ffa.com | www.261610.com | www.93955a.com | www.246hao.com | www.656022.com | www.86611l.com | 6261ee.com | 2222.ag | 670916.com | 3245i.com | www.320661.com | www.c1332.com | www.97655w.com | www.hg6833.com | www.xpj66696.com | www.568000.com | www.4167b.com | www.bet365de.com | 54443k.com | 61328822.com | 40014477.com | www5682.com | 22883n.com | 4444sands.com | 39552288.com | www.190883.com | www.810603.com | www.3126u.com | www.bet63.com | www.hr1855.com | www.4521d.com | www.47486.com | www.lfcp096.com | www.678987.com | www.xb0027.com | www.sky5678.com | www.e32126.cc | 1407o.com | feicai0756.com | 348977.com | zhe988.com | u01234.com | 4231b.com | 3568rr.com | 55797m.com | 3678mmm.com | 3473e.com | 88770076.com | u2146.com | 8827ttt.com | zb153.com | 547856.com | 454647.com | hay9256.com | 88772105.com | amjs119.com | 66300vip17.com | x83377.com | 80567k.com | 30171177.com | laojieyuhe.com | 77112007.com | zz4119.com | oo8159.cc | 4152u.com | 3950r.com | 22442007.com | 8406.com | 2844t.com | 2381xx.com | 50128a.com | www.56733h.com | www.hg290.com | www.yinhecc00.com | www.h7788o.com | www.www-16387.com | www.hg00111.com | www.16711.com | www.9374i.com | www.568000.com | www.1168k.com | www.v88139.com | www.79095r.com | www.senurcare.com | www.7886268.com | www.703097.com | www.43818.cc | www.973390.com | www.565516.com | www.106526.com | 6245p.com | 1577726.com | bwinkkk.com | 8037ss.com | 22117s.com | 2355m.com | www.v0030.com | www.caipiao045.com | www.82344.com | www.hjcp3.com | www.10999h.com | www.78680w.com | www.bet73r.com | www.2632m.com | www.673266.com | www.106135.com | 5200e.com | 9694.com | 4018e.com | 9498.biz | www.yinhecc99.com | www.838967.com | www.k9478.com | www.7366l.com | www.h6630.com | www.9149j.com | www.699494.com | 80368aa.com | 9339999s.com | 9737xx.me | a2554.com | www.f32126.cc | www.hg0189.com | www.19019q.com | www.556350.com | www.99048.cc | www.431506.com | 73999i.com | 61652j.com | 8406.com | www.yh7099.com | www.555507.com | www.1851131.com | www.7256.com | www.929709.com | 112k.net | 55331xs.com | df8r.com | www.gt885.com | www.js504.com | www.79095i.com | www.60108m.com | www.587989.com | 91019n.com | qqq40033.com | 058757.com | www.3775622.com | www.10999p.com | www.893267.com | www.661526.com | 88993u.com | 3566bb.com | www.o168a.com | www.kk377.com | www.614986.com | www.2934l.com | 29918y.com | 112s.net | 7945ii.com | www.vns799.com | www.00778k.com | www.34788m.com | www.90305a.com | zb153.com | 0434388.com | www.hg88996.com | www.ag3377.com | www.7777hi.com | 4444372.com | 4556q.com | www.5551388.net | www.hjcp55.com | www.16065z.com | www.489770.com | 1665m.com | 023625.com | www.49981k.com | www.63606j.com | www.hc2588.com | 31414.com | 1591vip.com | www.006071.com | www.73990f.com | www.c4526.com | x5816.com | 35oo.vip | www.flb0009.com | www.a32031.com | www.c1353.com | 1305007.com | dh2147.com | www.8k365.com | www.98xpj98.com | www.586840.com | 3559zzz.com | 1064.com | www.143291.com | www.23579f.com | 44225156.com | 655661199.com | www.33335375.com | www.zzyl69.com | www.276608.com | 1458k.com | www.56520j.com | www.91779c.com | www.807266.com | ww01365.com | www.ao699.com | www.bwinyz25.com | www.959122.com | 40014499.com | www.hj91888.com | www.788jyh.com | www.937816.com | 363369.com | www.660727.com | www.0021331.com | www.890995.com | wb299.com | www.360025.com | www.9895h.com | www.506739.com | yh22444.com | www.60688store.com | www.888.com | www.51515z.com | 365vip400.cc | www.381818.com | www.77114b.com | 3559ww.com | 5622x.com | www.5981j.com | www.684544.com | 33318f.com | www.pp3737.com | www.954321r.com | 98kbc.com | 2381k.com | www.2bet005.com | www.589044.com | 3640ee.com | www.vns916.com | www.61655c.com | 3522z.vip | www.8124z.com | www.js89x.vip | 80878r.com | 8036aa.com | www.yzcp2025.com | www.868955.com | 3778802.com | www.2287111.com | www.79955.com | 9149u.com | www.444jsc.com | www.44118g.com | 760ss.cc | 2546j.com | www.89677s.com | www.178652.com | 11yy8332.com | www.7415oo.com | www.444086.com | 579336.com | www.p5550.com | www.2109j.com | 97789.com | www.hg8357.com | www.8113.cc | vns8049.com | www.33225.com | www.76gcw.com | wns157.com | www.kk6444.com | www.jcai1.com | 3405rrr.com | www.2195777.com | www.887763.com | 82365x.com | www.r4042.com | www.565378.com | 44ll8332.com | www.116668.net | www.43131j.com | 7004.am | www.9187q.com | 2649k.com | www.570166.com | www.16878o.com | 55545g.com | www.66667666.com | www.269777a.com | 88903636.com | www.28000k.com | www.33588o.com | z88rr.com | www.kywns.205115.com | 1592u.com | www.xpj1969.com | www.c4525.com | 6766.com | www.00778a.com | xpj51333.com | www.h7788y.com | www.05500w.com | 67890d.com | www.98698c.com | bwin8bb.com | www.28758u.com | www.52303k.com | 4005.com | www.6776ee.com | 662229.com | www.4107v.com | www.3126c.com | 56132450.com | www.15k0048.com | qp168.shsxiwl.cn | www.66930055.com | www.c5907.com | jbb71.cc | www.12136y.com | 2365.com | www.667686.com | www.djcp234.com | www.0999js.com | www.68689z.com | 9068ee.com | www.b35tt.com | 18438cj.com | www.09856.com | www.568380.com | www.6033i.com | www.65707z.com | 27878vv.com | www.79095t.com | 78111199.com | www.597567a.com | www.89894w.com | www.0505v.com | www.07xyc.com | 2739.com | www.4331o.com | 66287c.com | www.3066bb.com | 6888zx.com | www.lefa3333.com | 838eee.com | www.s874.vip |