Java線程:并發協作-死鎖
?
線程發生死鎖可能性很小,即使看似可能發生死鎖的代碼,在運行時發生死鎖的可能性也是小之又小。
?
發生死鎖的原因一般是兩個對象的鎖相互等待造成的。
?
在《Java線程:線程的同步與鎖》一文中,簡述死鎖的概念與簡單例子,但是所給的例子是不完整的,這里給出一個完整的例子。
?
/**
* Java線程:并發協作-死鎖
*
* @author Administrator 2009-11-4 22:06:13
*/
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;
????????????????????????}
????????????????}
????????}
}
* Java線程:并發協作-死鎖
*
* @author Administrator 2009-11-4 22:06:13
*/
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;
????????????????????????}
????????????????}
????????}
}
?
下面死鎖的情況發生了,真是難得一見啊:
?
?
本文出自 “ 熔 巖 ” 博客,請務必保留此出處 http://lavasoft.blog.51cto.com/62575/222074
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
