Skip to content

Commit fd61e7e

Browse files
committed
第十章 避免活跃性危险
1 parent 768a0b7 commit fd61e7e

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.jay.thread.chap10;
2+
3+
/**
4+
* 动态的锁顺序死锁,容易发生死锁
5+
* 例如:如果两个线程同时调用transferMoney,
6+
* 其中一个线程从X向Y转账,另一个线程从Y向X转账,
7+
* 那么就会发生死锁
8+
* A:transferMoney(myAccount,yourAccount,10)
9+
* B:transferMoney(yourAccount,myAccount,20)
10+
* <p>
11+
* 如果执行时序不当,那么A可能获得myAccount的锁并等待yourAccount
12+
* 的锁,然而B此时持有yourAccount的锁,并正在等待myAccount的锁
13+
* Created by xiang.wei on 2017/8/28
14+
*/
15+
public class DemonstrateDeadlock {
16+
17+
public static void transferMoney(Account fromAccount,
18+
Account toAccount,
19+
DollarAmount amount) throws InsufficentFundsException {
20+
synchronized (fromAccount) {
21+
synchronized (toAccount) {
22+
if (fromAccount.getBalance().compareTo(amount)<0) {
23+
throw new InsufficentFundsException();
24+
} else {
25+
fromAccount.debit(amount);
26+
toAccount.credit(amount);
27+
}
28+
}
29+
}
30+
31+
}
32+
33+
static class DollarAmount implements Comparable<DollarAmount> {
34+
public DollarAmount add(DollarAmount amount) {
35+
return null;
36+
}
37+
38+
public DollarAmount subtract(DollarAmount amount) {
39+
return null;
40+
}
41+
42+
@Override
43+
public int compareTo(DollarAmount o) {
44+
return 0;
45+
}
46+
}
47+
48+
49+
static class Account {
50+
private DollarAmount balance;
51+
52+
void debit(DollarAmount amount) {
53+
balance = balance.subtract(amount);
54+
}
55+
56+
void credit(DollarAmount amount) {
57+
balance = balance.add(amount);
58+
}
59+
60+
public DollarAmount getBalance() {
61+
return balance;
62+
}
63+
}
64+
65+
static class InsufficentFundsException extends Exception {
66+
}
67+
68+
69+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.jay.thread.chap10;
2+
3+
/**
4+
* 在制定锁的顺序时,可以使用System.identityHashCode方法,该方法将返回有Object.hashCode
5+
* 返回的值。
6+
* Created by xiang.wei on 2017/8/28
7+
*/
8+
public class InduceLockOrder {
9+
10+
11+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.jay.thread.chap10;
2+
3+
/**
4+
* 容易发生死锁!锁顺序死锁
5+
* Created by xiang.wei on 2017/8/28
6+
*/
7+
public class LeftRightDeadlock {
8+
private final Object left = new Object();
9+
private final Object right = new Object();
10+
11+
public void leftRight() {
12+
synchronized (left) {
13+
synchronized (right) {
14+
doSomething();
15+
}
16+
}
17+
}
18+
19+
public void rightLeft() {
20+
synchronized (right) {
21+
synchronized (left) {
22+
doSomethingElse();
23+
}
24+
}
25+
}
26+
27+
public void doSomething() {
28+
}
29+
30+
public void doSomethingElse() {
31+
32+
}
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
**死锁**
2+
3+
例如线程A在等待线程B释放其持有的资源,而线程B永远都不释放该资源
4+
那么线程A就会一直等待下去。典型的例子就是"哲学家进餐"
5+
5个哲学家,5根筷子,并且每两个人中间放一根筷子。哲学家们
6+
时而思考,时而进餐。
7+
8+

0 commit comments

Comments
 (0)