• <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.333682.com | 8547ww.com | m333999.com | 55545e.com | 99306j.com | www.71071c.com | www.766352.com | bwin8.com | www.8124v.com | www.837993.com | www.127169.com | qjdy004.com | www.27363i.com | www.ct6676.com | yuhe.ooo | www.791555.com | www.07813.com | 19991e.com | www.32666y.com | www.91233h.com | 2287c.com | www.hg888.me | www.50080b.com | 44077k.com | www.3331p.com | www.96386b.com | 2566h2.com | www.58665h.com | 3616.com | www.wn2028.com | www.t98478.com | 11dd8332.com | www.558337.com | www.6364q.com | uuu4255.com | www.long987.com | www.cp208.com | 3202e.com | www.93919f.com | 1216.com | www.31276.com | www.433680.com | z2824.com | www.yh7771.com | feicai0475.com | 30019s.com | www.54400z.com | 554694.com | www.99113c.com | www.77803i.com | www.mgm8699.com | www.2418q.com | 7720796.com | www.0057g.com | jixiang20.com | www.xpj3666.com | www.679955.com | 7989z.com | www.4323q.com | p32365.com | www.3116b.com | 5360ee.com | www.7168805.com | www.bei01.com | 8905a.com | www.51550.cc | 26444f.com | www.0270a.com | ued888.com | www.33588b.com | 99332007.com | www.26163v.com | 50128e.com | www.68203dd.com | 4165e.com | www.wns123e.com | 838388c.com | www.099923.com | ff1331.com | www.199424.com | 8850.cc | www.5220666.com | 7196z.com | www.8015866.com | www.35918u.com | www.09569v.com | www.424988.com | www.tushan48.com | www.375769.com | www.hnjxjd.cn | bet777000.com | www.456qqq.com | dz22555.com | www.1010160.com | 38345t.com | www.99113p.com | 8722pppp.com | www.flb908.com | 9068kk.com | www.60007f.com | www.hg990077.com | www.217630.com | www.4763000.vip | www.665609.com | www.hg5538.com | www.234279.com | www.yh391.com | 1675gg.com | www.bd2019c.com | 1434a.com | www.109007.com | 998r.cc | www.00840i.com | 9411rrr.com | www.820380.com | www.7720o.com | www.50064a.com | www.888c31.com | 3778mm.com | www.bet63g.com | 5651k.com | www.45598u.com | 3454549.com | www.64141.com | www.30099.com | www.579020.com | www.7111f.com | 83377e.com | www.837993.com | 0080i.com | www.715585.com | www.73518.com | gfc1688.com | www.80065h.com | www.8181sun.com | www.52072o.com | www.9b006.com | 2138001.com | www.3qxc.com | www.fc841.com | 4002aa.com | www.34788k.com | www.dy1199.com | 3678kkk.com | www.xj7003.com | 51133oo.com | www.897060.com | www.44889u.com | 87665k.com | www.bjl916.com | 3049l.com | 1705r.com | www.883399i.com | 9649l.com | www.399160.com | www.89599z.com | 8159bbb.cc | www.c4523.com | www.vic040.com | 7003uu.com | www.708270.com | www.28281app.com | 2381yy.com | www.324229.com | www.82205.com | 3890q.com | www.c9337.com | www.11188807.com | 86611x.com | www.796933.com | www.ggt.99233t.com | 6261k.com | www.53911a.com | www.490804.com | www.yinhecc99.com | 00115003.com | www.456369.net | www.wns8858.com | 567bet.co | www.ampj3434.com | www.548sunbet.com | ll4255.com | www.101156.com | www.k32939.com | gd0621.com | vns7.vip | www.927586.com | www.0666pj.com | 032526.com | www.444070.com | www.8905r.com | www.hg6642.com | toucai55.com | www.912594.com | www.3116k.com | www.645suncity.com | 7605k.com | www.901512.com | www.b80288.com | 18438m.com | www.333222h.com | ab0056.com | 29918v.com | www.316686.com | www.606370.com | www.7793c.com | www.scfyou.com | www.3398711.com | www.63606i.com | www.2y931.com | www.48599.com | www.58777f.com | www.63877b.com | www.333xin2.com | 01234ll.com | 58w88.vip | 5855ff.com | 8577s.cc | 6766hh.com | www.66376v.com | www.332709.com | www.rrle1.com | www.702wb.com | www.5856861.com | www.97828i.vip | www.40288k.com | www.2544g9.com | www.79500x.com | www.c44yy.com | www.78854.com | www.444412.com | www.xb0022.com | www.yh18826.com | www.1600yy.com | www.hg001111.com | www.4938t.com | www.7435e.com | www.lhg555.com | www.3435999.com | www.pu2678.com | www.0636c.com | www.5319m.com | www.7893900.com | www.53516a.com | www.1770q.com | www.bet3650914.com | www.11223885.com | www.xpj2008.co | www.mgm868009.com | www.00778r.com | www.w84d.com | www.ytrich.com | www.0270k.com | www.9958c.com | www.878749.com | www.556742.com | www.202901.com | 8294c.com | xsj.com | rf036.com | swtyooo.com | 55323u.com | 8547ff.com | 131e.net | www.p76888.com | www.00037b.com | www.hg8197.com | www.61456.com | www.837722.com | www.209035.com | www.89386.com | www.5446y.com | www.4648.cc | www.yi654.com | www.542676.com | www.324770.com | 87965qq.com | 3258g.com | 8159n.cc | rbbet44.com | www.87680n.com | www.cs505.com | www.a9151e.com | www.417920.com | www.2655268.com | www.ch8123.com | www.136057.com | 1294m.com | 69445555.com | v1931.com | www.3371777.com | www.33js11.com | www.335217.com | www.w63568.com | www.89911.cc | www.81233u.com | js26732.com:9885 | 452.com | 3049f.com | www.535msc.com | www.97cp456.com | www.0055sun.com | www.bet365193.com | www.775780.com | dzc06.com | mg437711.com | 55vn77.com | www.903833.com | www.k8kk11.com | www.56011t.com | www.30xw.com | www.321676.com | 836858.com | 33382v.com | www.hg45888.com | www.005828.com | www.8905w.com | www.w635.com | 00q124.com | 2820o.com | 2844n.com | www.234567o.com | www.bet63z.com | www.25688l.com | www.73166i.com | 496ss.com | 3878722.com | www.yh33558.com | www.9822ae.com | www.004hy.cc | 87965ee.com | 3863.com | 00024066.com | www.5966iii.com | www.amdwc.net | www.506932.com | 1634r.com | bb888866.com | www.a7793.com | www.68568c.com | www.326608.com | 777hg999.com | www.4759ww.com | www.50999.com | www.26299h.com | 131hh.net | ff7742.com | 58802x.com | www.68277777.com | www.2588cai.com | www.160883.com | 32666a.com | w0595.com | www.25xz.com | www.52303f.com | feicai0379.com | yz5488.com | www.aa444.vip | www.299764.com | www.846661.com | 3863.com | www.21365qq.com | www.07679x.com | www.599280.com | 7616.net | www.7435o.com | www.38138z.com | www.817474.com | 3y5500.com | www.32126s.net | www.115527z.com | www.376357.com | 3473w.com | www.7111m.com | www.5953188.com | www.452804.com | m40033.com | www.xb0023.com | www.ya2019u.com | 168cp-s.com | r35151.com | www.57157.com | www.974288.com | 1705p.com | www.6033n.com | www.4996dl.com | www.635852.com | 3435f.com | www.828798.com | www.29277y.com | 364810.com | www.s67811.com | www.o63568.com | 168cp-h.com | 2211760.com | www.9570118.com | www.377506.com | 80368zz.com | www.hg1767.com | www.pj56ii.com | 3844yy.com | www.hg66668.cc | www.hd8674.com | 4323i.com | www.76060c.com | www.81608j.com | 3435l.com | www.888vip3.com | www.55717z.com | 3788z.com | www.7720g.com | www.917004.com | yth2.net | www.hg1767.com | www.36166m.com | 0080e.com | www.sun5507.com | www.80767p.com | 3679cc.com | www.446677.com | www.hqcp9.com | 36401122.com | www.858571.com | www.348116.com | 3225n.com | www.40288w.com | 00787.com | www.7380e.com | www.9989583.com | 1592u.com | www.tyc95.com | www.cf9907.com | 81207as3.com | www.80188u.com | 4182g.com | www.653507.com | www.hd5388.com | y00.com | www.25xz.com | www.178257.com | 11829455.com | www.46630.vip | 3258n.com | www.9679k.com | www.hh3890.com | 711066.com | www.v063801.com | 67877u.com | www.2078o.com | www.c145.vip | 1115673.com | www.615069.com | c3144.com | www.2078o.com | www.854766.com | 88807e.com | www.5856865.com | yth67.com | www.27363v.com | jbs6689.com | www.32666g.com | www.931670.com | 588.cc | www.hga99900.com | tb2223.com | www.pj88m.com | 95066666.com | www.88399.com | www.77803o.com | www.00618l.com | www.996732.com | 2247c.com | www.bmw1022.com | vip9923.com | www.5981e.com | 56988d.com | www.kl88ag.com | www.195090.com | www.bet3650614.com | www.hm5577.com | 87665i.com | www.35155m.com | 23800v.com | www.bet73j.com | 80850hh.com | www.sj5558.com | 8037i.com | www.749758.com | swin9.com | www.xpjylc008.com | 5099bb.com | www.5508598.com | 4340.com | www.7366002.com | 8381o.com | www.673888q.com | cr258.com | www.5446tt.com | 58802e.com | www.7415a.com | b1915.com | www.2737012.com | 838388i.com | www.x80288.com | js14.com | www.8905n.com | 3435f.com | www.68993269.com | w5556944.com | www.50732d.com | 2355f.com | www.qucw3.com | www.bobifa.net | www.606530.com | www.9679y.com | 4066yy.com | www.js9741.com | 12742v.com | www.yh8878b.com | 22463gong.com | www.cn365x.com | 6118m.com | www.3691z.com | 6766oo.com | www.65707z.com | 66003885.com | www.549077.com | www.9949k.com | 99wb8.com | www.8887hj.com | 054598.cc | www.323535.com | ff555j.com | www.055m.cc | www.746441.com | www.43131z.com | www.6666hs.com | 90307e.com | www.r999995.com | 4182006.com | www.27czj.com | www.76543y.com | 00774xx.com | www.1707222.com | ma76.com | www.yh7772.com | 881381.com | www.666405.com | www.hg2216.com | 8790t.com | www.1559503.com | 3522uu.com | www.22czj.com | www.bocainews.net | qq1331.com | www.357116.com | nn500e.com | www.hc2388.com | www.bmw1195.com | 998e.cc | www.68365f.com | 45637p.com | www.911888aa.com | www.xpj775.com | 8538y.com | www.82894.com | www.532858.com | 0166b.com | www.4058ii.com | 4809v.com | www.676944.com | www.5588212.com | 80892m.com | www.33997j.com | www.55268ll.com | 80850ee.com | www.4833041.com | www.111893.com | 98955i.com | www.06386677.com | 818132.com | www.088186.com | www.vns6601.com | 55331k.com | www.530477.com | www.55526o.com | 9994cc16.com | www.621329.com | www.kjlhc.cc | 53059.com | www.602857.com | www.t9478.com | 22442007.com | www.550284.com | www.79500a.com | hwcp.com | www.216750.com | www.bet63c.com | 2544b7.com | 4195dd.com | www.69567f.com | 28824e.com | 38821155.com | www.2418o.com | www.333133u.com | 162788.com | www.9149k.com | www.868216.com | www.wuxingcai.com | 00778s.com | 59964tt.com | www.78680h.com | www.6491g.com | yun889.com | www.0058c.com | www.d22365.com | 655661199.com | www.021067.com | www.40288j.com | www.pj8819.com | 2222k8.com | www.2875j.com | www.cc444.com | h51335.net | www.030310.com | www.xpj8869.com | www.943a28.com | ee38648.com | www.812780.com | www.848777w.com | 2008nn.com | 88119n.com | www.3668t.com | www.09766.com | 80368rr.com | 28288ii.com | www.yun588.com | www.9822msc.com | 3559xx.com | zhcw.com | www.06660.com | www.0860h.com | fc37.com | www.50024e.com | www.260655.com | www.1007b.cc | 61325500.com | 4635588.com | www.33997p.com | www.5360ss.com | jjfysc.com | 3559zzz.com | www.725898.com | www.627464.com | www.09569d.com | 8742u.com | 445.com | www.49956w.com | www.0999888.com | www.0967111.com | hg008.am | www.160326.com | www.2324.com | www.009959.com | vip67778.com | 56787aa.com | www.178651.com | www.xj456789.com | www.55526r.com | www.fun08.net | 89777r.com | 4195y.com | www.53900h.com | www.9b008.com | www.h7788z.com | 11005l.com | 20040.com | www.863630.com | www.5446g.com | www.992254.com | 256777.com | spj05.net | bwin990.co | www.02022.com | www.80188n.com | www.572433.com | 8905u.com | 59599s.com | bet99929.com | www.5522e.cc | www.466890.com | www.8k365.com | hg0088.com | 28288s.com | pic.caibw1.com | www.sxyl3.com | www.8905j.com | www.118sjz.cc | www.65533vip.com | 7720t.net | 1331o.com | www.187513.com | www.22303.cc | www.42070010.com | www.2011.vip | www.hg558.com | 776091.com | 33432w.com | 2709f.com | www.760320.com | www.106795.com | www.net567.com | www.015456.com | www.dfs995.com | 3685l.com | 55331xs.com | 365499.bet | www.526733.com | www.60007l.com | www.js89h.vip | www.303015.me | www.55002055.com | 87680q.com | 112m.net | 19880v.com | 1144589.com | www.083552.com | www.hr4888.com | www.7239j.com | www.j063801.com | www.883122dd.com | www.66930055.com | www.8807js.com | 33318h.com | vns00vv.com | 131uu.net | f61653.com | www.303482.com | www.982570.com | www.50024y.com | www.zzyl63.com | www.b35bb.com | www.383806.com | www.36677c.com | hggjtg9.com | 3413222.com | 77606wcom | 992222o.com | 3775w.com | 58588.com | www.375781.com | www.914077.com | www.3416g.com | www.84499b.com | www.3775n.com | www.a12189.com | www.4212g.com | www.223349.com | www.56855r.com | www.2626sun.com | www.kb7070.com | bet3659092.com | 2306u.com | 667766h.com | 4182l.com | 7811o.com | 30019aa.com | 89892mm.com | g2649.com | ydgj0011.com | 20777711.com | 2698.com | p1458.com | 2677lll.com | vns8r.com | qg0707.com | js111js.com | jnbzsj.com | h88983.com | yhneimenggu.vip | 1479a.com | 28837711.com | 50099n.com | 44pp8332.com | 3991100.com | 98955g.com | 6150y.com | 2546n.com | b3144.com | 22yy8332.com | 3552d.com | 5369e.com | 496dd.com | 78808l.com | 77605k.com | toucai55.com | sha134.com | shb777.com | 407.com | 333222.com | c15666.com | jing822.com | 5309x.com | 65005n.com | zhcp73.com | www.sjs06.com | www.1019990.com | www.56520a.com | www.msc111.com | www.2091p.com | www.82627.com | www.6655288.com | www.00708.com | www.agcpw.com | www.8861tt.com | www.bet63e.com | www.0600u.cc | www.37377l.com | www.68682f.com | www.1035f.com | www.773577.com | www.293255.com | 2334vip1.com | 8827ll.com | 4066kk.com | 092258.com | 00vv8331.com | 9103ww.com | www.s32126.cc | www.pj8470.com | www.09766.com | www.4963z.com | www.73990z.com | www.567111.net | www.69998.cc | www.799346.com | www.77996e.com | 1770f.com | 2247tt.com | cais777.com | 2844j.com | www.yd555888.com | www.11p1.cc | www.938q.cc | www.l32031.com | www.0134008.com | www.064wy.com | www.422689.com | ei59.cn | y8381.com | 08159h.com | lehu808.com | www.98am444.com | www.kj306.com | www.469705.com | www.bet73b.com | www.hy5507.com | www.234892.com | 068789.com | 78666x.com | 0747y.com | www.205611.com | www.hg8104.com | www.208432.com | www.00840o.com | www.652776.com | 7935v.com | 99973.com | 9895t.com | www.7830x.com | www.5504ii.com | www.qiji111.com | www.ya717.com | www.36166u.com | qqq3405.com | dz883.com | www.4107c.com | www.a81h.cc | www.bmw8044.com | 3413377.com | t1458.com | 50000022.com | www.29886l.com | www.8860tt.com | www.162323.com | www.607269.com | 2287c.com | 4255i.com | www.tushan08.com | www.6a444.com | www.5856858.com | www.679957.com | 4066r.com | 2jszzz.com | www.y30226.com | www.89599o.com | www.50080j.com | www.50024a.com | 99909k.com | 1479r.com | www.16602i.com | www.6939l.com | www.426667.com | 3467h.cc | 99151n.com | www.785888.com | www.42456677.com | www.683944.com | v556677.com | 97q.com | www.796778.com | www.0011sun.com | www.711895.com | 644237.com | 3089g.com | www.500994.com | www.7681003.com | www.585868.cc | 2287hh.com | 8898111.com | www.46615.com | www.3333bj.com | www.77802o.com | 3559pp.com | www.55268xx.com | www.xj7004.com | www.820737.com | 5156.com | P35pp.com | www.56655z.com | www.68682r.com | 69111xpj.com | wns950.com | www.hg1161.com | www.9996yy.com | www.187513.com | 2214o.com | www.e4737.com | www.30350z.com | www.755096.com | 94111ddd.com | www.s8866.com | www.5981i.com | www.965160.com | y888.com | www.6806pj.com | www.k69096.com | www.910882.com | 4136.com | www.0853g.com | www.4996hs.com | www.785579.com | 83138x.com | www.88807k.com | www.52062x.com | www.029079.com | 8801885.com | www.505444.com | www.83993s.com | 3305596.com | wancai.com | www.50999f.com | www.959709.com | 59599f.com | www.87680s.com | www.zzyl64.com | 33tt8332.com | sha0003.com | www.7335zz.com | www.47d.cc | 8294y.com | www.g32126.cc | www.jsbet008.com | 7334t.com | 3049l.com | www.7225h.com | www.345576.com | 4182h.com | www.49yj.com | www.hy2811.com | 8557i.com | www.83008f.com | www.81520q.com | 11bwi.com | www.xpj2278.com | www.48330j.com | 20188w.com | www.517ly13.com | www.52062h.com | 6175uu.com | uu38648.com | www.4521p.com | www.50732a.com | 444ths.com | www.4996gs.com | www.71399v.com | x8877.com | www.b35oo.com | www.189909.com | 2677www.com | www.hg8vv.com | www.191061.com | feicai0477.com | www.80188z.com | 4412.com | js90055.com | www.89386a.com | 2643c.com | www.9844ii.com | www.42456688.com | 1665n.com | www.10051122.com | www.83993d.com | 8006009.com | www.765543.com | www.55cmc.com | 82365s.com | www.56766.com | www.256968.com | 2698j.com | www.86339s.com | 9949n.com | www.21365ll.com | www.00840d.com | 78666x.com | www.85857n.com | www.230961.com | feicai0470.com | www.5555287.com | feicai0752.com | www.25288m.com | www.66ffu.com | 8159s.cc | www.57800t.com | 30006k.com | www.753055.com | www.60123j.com | 3807o.com | www.cai32.com | www.35918a.com | www.hg6789.com | www.81678l.com | z1458.com | www.5504w.com | 4136d.com | www.vns68.com | www.9818g.cc | 4541e.com | www.b35xx.com | 9995z.cc | www.888423.net | www.663566.com | 3121x.com | www.16065b.com | xpjggzz.com | www.834817.com | n00351.com | www.am5544.com | www.930557.com | 2190m.com | www.pjzxyl.cc | 8381hh.com | www.5446oo.com | bet3659091.com | www.894929.com | www.314213.com | www.7435g.com | www.918ae.com | tony001.com | www.yuehengsz.com | ss1915.com | www.85857n.com | 8742q.com | www.81855.com | www.322424.com | www.vns0n0.com | www.qucp4.com | 55899b.com | www.52072r.com | 2146i.com |