第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

lock.tryLock()方法的使用

lock.tryLock()方法的使用

九州編程 2019-03-12 13:10:51
package concurrent;import java.util.ArrayList;import java.util.List;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class TestTryLock {    private List<Object> list = new ArrayList<Object>();    private Lock         lock = new ReentrantLock();    public static void main(String[] args) {        final TestTryLock test = new TestTryLock();        new Thread("第一個(gè)線程  ") {            @Override            public void run() {                test.doSomething(Thread.currentThread());            }        }.start();        new Thread("第二個(gè)線程  ") {            @Override            public void run() {                test.doSomething(Thread.currentThread());            }        }.start();    }    public void doSomething(Thread thread) {        if (lock.tryLock()) {            try {                System.out.println(thread.getName() + "得到了鎖.");                for (int i = 0; i < 10; i++) {                    list.add(i);                }            } catch (Exception e) {                e.printStackTrace();            } finally {                System.out.println(thread.getName() + "釋放了鎖.");                lock.unlock();            }        } else {            System.out.println(thread.getName() + "獲取鎖失敗.");        }    }}以上代碼運(yùn)行結(jié)果如下:第一個(gè)線程  得到了鎖.第一個(gè)線程  釋放了鎖.第二個(gè)線程  得到了鎖.第二個(gè)線程  釋放了鎖.package concurrent;import java.util.ArrayList;import java.util.List;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class TestTryLock {    private List<Object> list = new ArrayList<Object>();    private Lock         lock = new ReentrantLock();運(yùn)行結(jié)果如下:第二個(gè)線程  得到了鎖.第一個(gè)線程  獲取鎖失敗.第二個(gè)線程  釋放了鎖.問題如下:我知道lock()方法去獲取鎖,當(dāng)獲取不到鎖的時(shí)候,會(huì)一直等待。直到獲取到鎖。tryLock()方法獲取鎖的時(shí)候,制作一次試探,如果獲取鎖失敗,就不會(huì)一直等待的。如果是這樣的話,如我Demo所示的這樣,在業(yè)務(wù)邏輯中使用tryLock很容易造成程序不可控。比較疑惑這個(gè)tryLock的使用方法。。求大神解釋。。謝謝~~
查看完整描述

4 回答

?
夢(mèng)里花落0921

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊

這個(gè)最好把Lock的四個(gè)鎖法都比較一下(容我copy些東西):


  • void lock();

If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.

在等待獲取鎖的過程中休眠并禁止一切線程調(diào)度


  • void lockInterruptibly() throws InterruptedException;

If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
The lock is acquired by the current thread; or Some other thread interrupts the current thread, and interruption of lock acquisition is supported.

在等待獲取鎖的過程中可被中斷


  • boolean tryLock();

Acquires the lock if it is available and returns immediately with the value true. If the lock is not available then this method will return immediately with the value false.

獲取到鎖并返回true;獲取不到并返回false


*boolean tryLock(long time, TimeUnit unit) throws InterruptedException;

If the lock is available this method returns immediately with the value true. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:The lock is acquired by the current thread; or Some other thread interrupts the current thread, and interruption of lock acquisition is supported; or The specified waiting time elapses.

在指定時(shí)間內(nèi)等待獲取鎖;過程中可被中斷


假如線程A和線程B使用同一個(gè)鎖LOCK,此時(shí)線程A首先獲取到鎖LOCK.lock(),并且始終持有不釋放。如果此時(shí)B要去獲取鎖,有四種方式:

  • LOCK.lock(): 此方式會(huì)始終處于等待中,即使調(diào)用B.interrupt()也不能中斷,除非線程A調(diào)用LOCK.unlock()釋放鎖。

  • LOCK.lockInterruptibly(): 此方式會(huì)等待,但當(dāng)調(diào)用B.interrupt()會(huì)被中斷等待,并拋出InterruptedException異常,否則會(huì)與lock()一樣始終處于等待中,直到線程A釋放鎖。

  • LOCK.tryLock(): 該處不會(huì)等待,獲取不到鎖并直接返回false,去執(zhí)行下面的邏輯。

  • LOCK.tryLock(10, TimeUnit.SECONDS):該處會(huì)在10秒時(shí)間內(nèi)處于等待中,但當(dāng)調(diào)用B.interrupt()會(huì)被中斷等待,并拋出InterruptedException。10秒時(shí)間內(nèi)如果線程A釋放鎖,會(huì)獲取到鎖并返回true,否則10秒過后會(huì)獲取不到鎖并返回false,去執(zhí)行下面的邏輯。


是否會(huì)造成 程序不可控, 不在于這幾種方式本身,在于業(yè)務(wù)類別和使用邏輯上。


查看完整回答
反對(duì) 回復(fù) 2019-04-19
?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

為什么說會(huì)造成程序不可控呢,調(diào)用tryLock方法之后會(huì)立即返回結(jié)果,根據(jù)是否獲得鎖然后做出相應(yīng)的業(yè)務(wù)操作,相比較于lock方法會(huì)一直阻塞這本身已經(jīng)使程序更可控了。


查看完整回答
反對(duì) 回復(fù) 2019-04-19
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

不同的方法有不同的用處啊,只是應(yīng)用的場(chǎng)景不同,不是不可控


查看完整回答
反對(duì) 回復(fù) 2019-04-19
?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

題主這里對(duì)程序可控的理解是指每一個(gè)任務(wù)都按照要求執(zhí)行,但我覺得既然用tryLock()這個(gè)非阻塞的場(chǎng)景,就是要允許某些任務(wù)不執(zhí)行(比如防止重復(fù)提交業(yè)務(wù)),或超時(shí)不執(zhí)行(比如防止資源等待隊(duì)列溢出)等。


查看完整回答
反對(duì) 回復(fù) 2019-04-19
  • 4 回答
  • 0 關(guān)注
  • 3422 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)