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

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

死鎖的例子

死鎖的例子

一只斗牛犬 2023-06-04 19:42:10
我檢查了很多網(wǎng)站,唯一的例子deadlock就是這樣。synchronized塊中總有塊synchronized。(方法withdraw被鎖定a,方法deposit被鎖定b。)class Account{    int balance;    Account(int amount)    {balance = amount;}    void withdraw(int amount)    {balance-=amount;}    void deposit(int amount)    {balance+=amount;}}class Threaddemo extends Thread{    Account a,b;    int amount;    Threaddemo(Account a,Account b,int amount)    {        this.a=a;this.b=b;this.amount=amount;        start();    }    public void run()    {        transfer(a,b,amount);    }    public void transfer(Account a,Account b,int amount)    {        synchronized(a)        {            a.withdraw(amount);            System.out.print(amount+" is withdrawn from account a\n");            try{Thread.sleep(500);}            catch(Exception e){System.out.println(e);}            synchronized(b)            {                b.deposit(amount);                System.out.print(amount+" is deposited into account b\n");            }        }    }}class U3{    public static void main(String[] args)     {        Account a = new Account(1000);        Account b = new Account(2000);        new Threaddemo(a,b,100);        new Threaddemo(b,a,200);    }}但是如果我們?cè)谝粋€(gè)同步塊之后使用一個(gè)同步塊,就不會(huì)有死鎖。如果這是導(dǎo)致死鎖的唯一方法,那我們?yōu)槭裁床皇褂脙蓚€(gè)單獨(dú)的同步塊呢?如果還有其他方法導(dǎo)致死鎖,請(qǐng)舉例說(shuō)明。
查看完整描述

1 回答

?
江戶(hù)川亂折騰

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

考慮一家擁有數(shù)千個(gè)類(lèi)型的銀行賬戶(hù)的銀行Account?,F(xiàn)在讓我們看看為什么這段代碼會(huì)導(dǎo)致死鎖:


public void transfer(Account a,Account b,int amount)

    {

        synchronized(a)

        {

            a.withdraw(amount);

            System.out.print(amount+" is withdrawn from account a\n");


            try{Thread.sleep(500);}

            catch(Exception e){System.out.println(e);}


            synchronized(b)

            {

                b.deposit(amount);

                System.out.print(amount+" is deposited into account b\n");

            }

        }

    }

讓有線(xiàn)程tA和線(xiàn)程tB。如果線(xiàn)程在線(xiàn)程同時(shí)運(yùn)行時(shí)tA運(yùn)行以下代碼 ,則可能會(huì)出現(xiàn)死鎖,因?yàn)槿绻覀儾榭匆韵马樞颍?code>transfer(accountA, AccountB)tBtransfer(accountB, accountA)

  1. 面向:synchronized(accountA)

  2. 待定:synchronized(accountB)

  3. tA:嘗試鎖定對(duì)象AccountB,但鎖定由 tB 持有 =>死鎖

我們看到兩者之間存在循環(huán)依賴(lài)關(guān)系,不允許其中一個(gè)線(xiàn)程繼續(xù)前進(jìn)。

如果我們查看您更新的代碼:

public void transfer(Account a,Account b,int amount)

    {

        synchronized(a)

        {

            a.withdraw(amount);

            System.out.print(amount+" is withdrawn from account a\n");


            try{Thread.sleep(500);}

            catch(Exception e){System.out.println(e);}

        }

        synchronized(b)

        {

            b.deposit(amount);

            System.out.print(amount+" is deposited into account b\n");

        }

    }

我們必須采取以下假設(shè):

  • 賬戶(hù) a 有無(wú)限的資金,因?yàn)樗赡苁?a.balance < amount,這意味著 a.balance < 0,這打破了我們總是有余額 >=0 的不變量。

    • 我們?cè)试S不一致,例如,如果我們想?yún)R總所有當(dāng)前現(xiàn)金,我們將匯總少于實(shí)際金額,因?yàn)槟?dāng)前的代碼允許我們這樣做。

如果我們嘗試修復(fù)代碼,我們必須在更新余額之前確保 a.balance >= amount?,F(xiàn)在讓我們看看以下場(chǎng)景:

  1. 賬戶(hù)abalance < amount

  2. 我們必須等到a.balance >= amount從帳戶(hù)中取款a

  3. 因?yàn)槲覀冊(cè)谶@個(gè)線(xiàn)程中保持 Account 的鎖a,所以沒(méi)有其他線(xiàn)程可以更新a.balance=> 我們?cè)馐?strong>饑餓

要解決這些問(wèn)題,您要么必須使用監(jiān)視器或條件來(lái)檢查 a.balance>=amount 是否要進(jìn)行,并將線(xiàn)程置于阻塞狀態(tài),以便線(xiàn)程可以進(jìn)行或更新您的代碼,以便鎖定總是以相同的順序獲取。

解決方案#1:獲取對(duì)象鎖的唯一順序

如果我們使用唯一的順序獲取對(duì)象的鎖,我們可以確保不會(huì)發(fā)生死鎖,因?yàn)槲覀円灾付ǖ捻樞颢@取鎖,不允許任何循環(huán)依賴(lài),否則稱(chēng)為死鎖。

public void transfer(Account a,Account b,int amount)

    {

       //define a specific order, in which locks are acquired

       //the id's of all accounts are unique!

       if(a.id<b.id){

          synchronized(a){

            synchronized(b){

               //do operations here

            }

          }

       }else{

          synchronized(b){

            synchronized(a){

               //do operations here

            }

          }

       }

    }

解決方案 #2:使用生產(chǎn)者-消費(fèi)者模式來(lái)檢查a.balance>=amount.


public void transfer(Account a,Account b,int amount)

{

    while(true){

      synchronized(a){

          if(a.balance>=amount){

              //do operations here

          }

      }


        try{Thread.sleep(500);} //Use this as a backoff, as otherwise you'd likely get high congestion

        catch(Exception e){System.out.println(e);}

    }

    synchronized(b)

    {

       //do operations here

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-04
  • 1 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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