線程同步——死鎖

在實際編程中,死鎖雖然不常見,可是若是遇到死鎖即是致命的。接下來了解一下《操做系統》對於死鎖產生條件的描述。java

死鎖產生的條件:編程

(1)互斥條件:所謂互斥就是進程在某一時間內獨佔資源。ide

(2)請求與保持條件:一個進程因請求資源而阻塞時,對已得到的資源保持不放。this

(3)不剝奪條件:進程已得到資源,在末使用完以前,不能強行剝奪。操作系統

(4)循環等待條件:若干進程之間造成一種頭尾相接的循環等待資源關係。.net

根據上述四個條件咱們來編寫一個線程死鎖的例子:線程

public class Test { 
        public static void main(String[] args) { 
                DeadlockRisk dead = new DeadlockRisk(); 
                MyThread t1 = new MyThread(dead, 1, 2); 
                MyThread t2 = new MyThread(dead, 3, 4); 
                MyThread t3 = new MyThread(dead, 5, 6); 
                MyThread t4 = new MyThread(dead, 7, 8); 

                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) { 
                this.dead = dead; 
                this.a = a; 
                this.b = b; 
        } 

        @Override 
        public void run() { 
                dead.read(); 
                dead.write(a, b); 
        } 
} 

class DeadlockRisk { 
        private static class Resource { 
                public int value; 
        } 

        private Resource resourceA = new Resource(); 
        private Resource resourceB = new Resource(); 

        public int read() { 
                synchronized (resourceA) { 
                        System.out.println("read():" + Thread.currentThread().getName() + "獲取了resourceA的鎖!"); 
                        synchronized (resourceB) { 
                                System.out.println("read():" + Thread.currentThread().getName() + "獲取了resourceB的鎖!"); 
                                return resourceB.value + resourceA.value; 
                        } 
                } 
        } 

        public void write(int a, int b) { 
                synchronized (resourceB) { 
                        System.out.println("write():" + Thread.currentThread().getName() + "獲取了resourceA的鎖!"); 
                        synchronized (resourceA) { 
                                System.out.println("write():" + Thread.currentThread().getName() + "獲取了resourceB的鎖!"); 
                                resourceA.value = a; 
                                resourceB.value = b; 
                        } 
                } 
        } 
}
//運行結果
read():Thread-0獲取了resourceA的鎖!
read():Thread-0獲取了resourceB的鎖!
write():Thread-0獲取了resourceA的鎖!
read():Thread-3獲取了resourceA的鎖!

上面的執行結果基本能夠看出死鎖,咱們能夠看一下線程dumpcode

Java stack information for the threads listed above:
===================================================
"Thread-3":
	at com.thread.base.dead.DeadlockRisk.read(DeadlockRisk.java:16)
	- waiting to lock <0x00000007957972e8> (a com.thread.base.dead.DeadlockRisk$Resource)
	at com.thread.base.dead.MyThread.run(MyThread.java:18)
"Thread-2":
	at com.thread.base.dead.DeadlockRisk.read(DeadlockRisk.java:18)
	- waiting to lock <0x00000007957972f8> (a com.thread.base.dead.DeadlockRisk$Resource)
	- locked <0x00000007957972e8> (a com.thread.base.dead.DeadlockRisk$Resource)
	at com.thread.base.dead.MyThread.run(MyThread.java:18)
"Thread-0":
	at com.thread.base.dead.DeadlockRisk.write(DeadlockRisk.java:28)
	- waiting to lock <0x00000007957972e8> (a com.thread.base.dead.DeadlockRisk$Resource)
	- locked <0x00000007957972f8> (a com.thread.base.dead.DeadlockRisk$Resource)
	at com.thread.base.dead.MyThread.run(MyThread.java:19)

Found 1 deadlock.//發現死鎖

更多關於線程dump能夠參考:http://my.oschina.net/u/161458/blog/266313orm

相關文章
相關標籤/搜索