Java多線程:線程死結

來源:互聯網
上載者:User

發生死結的原因一般是兩個對象的鎖相互等待造成的。
下面用一個執行個體來構造這種情況:

package basic.e_deadlock;import org.apache.log4j.Logger;public class TestDeadLock {public static void main(String[] args) {DeadlockRisk dead = new DeadlockRisk();MyThread t1 = new MyThread(dead, 1, 2, "線程1");MyThread t2 = new MyThread(dead, 3, 4, "線程2");MyThread t3 = new MyThread(dead, 5, 6, "線程3");MyThread t4 = new MyThread(dead, 7, 8, "線程4");t1.start();t2.start();t3.start();t4.start();}}class MyThread extends Thread {private DeadlockRisk dead;private int a, b;MyThread(DeadlockRisk dead, int a, int b, String threadName) {this.dead = dead;this.a = a;this.b = b;this.setName(threadName);}@Overridepublic void run() {dead.read();dead.write(a, b);}}class DeadlockRisk {private static Logger logger = Logger.getLogger(DeadlockRisk.class);private static class Resource {public int value;}private Resource resourceA = new Resource();private Resource resourceB = new Resource();public void read() {logger.debug("===========read  begin===========");synchronized (resourceA) {logger.debug("read():" + Thread.currentThread().getName() + "擷取了resourceA 的鎖!");synchronized (resourceB) {logger.debug("read():" + Thread.currentThread().getName() + "擷取了resourceB 的鎖!");}}logger.debug("===========read  end=============");}public void write(int a, int b) {logger.debug("===========write begin===========");synchronized (resourceB) {logger.debug("write():" + Thread.currentThread().getName() + "擷取了resourceB 的鎖!");synchronized (resourceA) {logger.debug("write():" + Thread.currentThread().getName() + "擷取了resourceA 的鎖!");resourceA.value = a;resourceB.value = b;}}logger.debug("===========write end=============");}}

執行結果:

0    [線程1] DEBUG basic.e_deadlock.DeadlockRisk - ===========read  begin===========0    [線程2] DEBUG basic.e_deadlock.DeadlockRisk - ===========read  begin===========0    [線程4] DEBUG basic.e_deadlock.DeadlockRisk - ===========read  begin===========0    [線程3] DEBUG basic.e_deadlock.DeadlockRisk - ===========read  begin===========0    [線程1] DEBUG basic.e_deadlock.DeadlockRisk - read():線程1擷取了resourceA 的鎖!0    [線程1] DEBUG basic.e_deadlock.DeadlockRisk - read():線程1擷取了resourceB 的鎖!0    [線程1] DEBUG basic.e_deadlock.DeadlockRisk - ===========read  end=============0    [線程2] DEBUG basic.e_deadlock.DeadlockRisk - read():線程2擷取了resourceA 的鎖!0    [線程1] DEBUG basic.e_deadlock.DeadlockRisk - ===========write being===========1    [線程2] DEBUG basic.e_deadlock.DeadlockRisk - read():線程2擷取了resourceB 的鎖!1    [線程2] DEBUG basic.e_deadlock.DeadlockRisk - ===========read  end=============1    [線程2] DEBUG basic.e_deadlock.DeadlockRisk - ===========write being===========1    [線程1] DEBUG basic.e_deadlock.DeadlockRisk - write():線程1擷取了resourceB 的鎖!1    [線程4] DEBUG basic.e_deadlock.DeadlockRisk - read():線程4擷取了resourceA 的鎖!

注意:此時線程1在等待resourceB的資源,線程2在等待resourceA的資源。兩個線程在相互等待,出現死結。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.