Skip to content

Commit 9bc7f72

Browse files
committed
Thread deadlock
1 parent 51b9870 commit 9bc7f72

6 files changed

Lines changed: 110 additions & 105 deletions

File tree

threaddeadlock/Account.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
public class Account {
2-
private final String name;
3-
private double balance;
2+
private final String name;
3+
private double balance;
44

5-
public Account(String name) {
6-
this.name = name;
7-
}
5+
public Account(String name) {
6+
this.name = name;
7+
}
88

9-
public void withdraw(double amount) {
10-
this.balance -= amount;
11-
}
9+
public void withdraw(double amount) {
10+
this.balance -= amount;
11+
}
1212

13-
public void deposit(double amount) {
14-
this.balance += amount;
15-
}
13+
public void deposit(double amount) {
14+
this.balance += amount;
15+
}
1616

17-
public double getBalance() {
18-
return this.balance;
19-
}
17+
public double getBalance() {
18+
return this.balance;
19+
}
2020

21-
@Override
22-
public String toString() {
23-
return name;
24-
}
21+
@Override
22+
public String toString() {
23+
return name;
24+
}
2525
}

threaddeadlock/Main.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
public class Main {
2-
public static void main(String[] args) {
3-
final Account accA = new Account("Acc 1");
4-
final Account accB = new Account("Acc 2");
5-
accA.deposit(1000.00);
6-
accB.deposit(1000.00);
2+
public static void main(String[] args) {
3+
final Account accA = new Account("Acc 1");
4+
final Account accB = new Account("Acc 2");
5+
accA.deposit(1000.00);
6+
accB.deposit(1000.00);
77

8-
Transaction t1 = new Transaction("T01", accA, accB, 100.00);
9-
Transaction t2 = new Transaction("T02", accB, accA, 500.00);
8+
Transaction t1 = new Transaction("T01", accA, accB, 100.00);
9+
Transaction t2 = new Transaction("T02", accB, accA, 500.00);
1010

11-
t1.start();
12-
t2.start();
13-
}
11+
t1.start();
12+
t2.start();
13+
}
1414
}

threaddeadlock/Transaction.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
public class Transaction extends Thread {
2-
private final String id;
3-
private final Account from;
4-
private final Account to;
5-
private final double amount;
2+
private final String id;
3+
private final Account from;
4+
private final Account to;
5+
private final double amount;
66

7-
public Transaction(String id, Account from, Account to, double amount) {
8-
this.id = id;
9-
this.from = from;
10-
this.to = to;
11-
this.amount = amount;
12-
}
7+
public Transaction(String id, Account from, Account to, double amount) {
8+
this.id = id;
9+
this.from = from;
10+
this.to = to;
11+
this.amount = amount;
12+
}
1313

14-
@Override
15-
public void run() {
16-
synchronized (from) {
17-
from.withdraw(amount);
18-
try {
19-
Thread.sleep(500);
20-
} catch (InterruptedException e) {
21-
}
22-
synchronized (to) {
23-
to.deposit(amount);
24-
}
25-
}
26-
System.out.println(from + " is released by " + id);
27-
System.out.println(amount + "is transfered from " + from + " to " + to);
28-
}
14+
@Override
15+
public void run() {
16+
// Acquire the lock of Account 'from'
17+
synchronized (from) {
18+
from.withdraw(amount);
19+
try {
20+
Thread.sleep(500);
21+
} catch (InterruptedException e) { }
22+
23+
// Acquire the lock of Account 'to'
24+
synchronized (to) {
25+
to.deposit(amount);
26+
}
27+
// Release the lock of Account 'to'
28+
}
29+
// Release the lock of Account 'from'
30+
System.out.println(amount + "is transfered from " + from + " to " + to);
31+
}
2932
}

threaddeadlock/fixed/Account.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
public class Account {
2-
private final String name;
3-
private double balance;
2+
private final String name;
3+
private double balance;
44

5-
public Account(String name) {
6-
this.name = name;
7-
}
5+
public Account(String name) {
6+
this.name = name;
7+
}
88

9-
public void withdraw(double amount) {
10-
this.balance -= amount;
11-
}
9+
public void withdraw(double amount) {
10+
this.balance -= amount;
11+
}
1212

13-
public void deposit(double amount) {
14-
this.balance += amount;
15-
}
13+
public void deposit(double amount) {
14+
this.balance += amount;
15+
}
1616

17-
public double getBalance() {
18-
return this.balance;
19-
}
17+
public double getBalance() {
18+
return this.balance;
19+
}
2020

21-
@Override
22-
public String toString() {
23-
return name;
24-
}
21+
@Override
22+
public String toString() {
23+
return name;
24+
}
2525
}

threaddeadlock/fixed/Main.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
public class Main {
2-
public static void main(String[] args) {
3-
final Account accA = new Account("Acc 1");
4-
final Account accB = new Account("Acc 2");
5-
accA.deposit(1000.00);
6-
accB.deposit(1000.00);
2+
public static void main(String[] args) {
3+
final Account accA = new Account("Acc 1");
4+
final Account accB = new Account("Acc 2");
5+
accA.deposit(1000.00);
6+
accB.deposit(1000.00);
77

8-
Transaction t1 = new Transaction("T01", accA, accB, 100.00);
9-
Transaction t2 = new Transaction("T02", accB, accA, 500.00);
8+
Transaction t1 = new Transaction("T01", accA, accB, 100.00);
9+
Transaction t2 = new Transaction("T02", accB, accA, 500.00);
1010

11-
t1.start();
12-
t2.start();
13-
}
11+
t1.start();
12+
t2.start();
13+
}
1414
}
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
public class Transaction extends Thread {
2-
private final String id;
3-
private final Account from;
4-
private final Account to;
5-
private final double amount;
2+
private final String id;
3+
private final Account from;
4+
private final Account to;
5+
private final double amount;
66

7-
public Transaction(String id, Account from, Account to, double amount) {
8-
this.id = id;
9-
this.from = from;
10-
this.to = to;
11-
this.amount = amount;
12-
}
7+
public Transaction(String id, Account from, Account to, double amount) {
8+
this.id = id;
9+
this.from = from;
10+
this.to = to;
11+
this.amount = amount;
12+
}
1313

14-
@Override
15-
public void run() {
16-
synchronized (from) {
17-
from.withdraw(amount);
18-
try {
19-
Thread.sleep(500);
20-
} catch (InterruptedException e) {
21-
}
14+
@Override
15+
public void run() {
16+
// Acquire the lock of Account 'from'
17+
synchronized (from) {
18+
from.withdraw(amount);
19+
try {
20+
Thread.sleep(500);
21+
} catch (InterruptedException e) { }
2222
}
23-
synchronized (to) {
24-
to.deposit(amount);
25-
}
26-
System.out.println(from + " is released by " + id);
27-
System.out.println(amount + "is transfered from " + from + " to " + to);
28-
}
23+
// Release the lock of Account 'from'
24+
// Acquire the lock of Account 'to'
25+
synchronized (to) {
26+
to.deposit(amount);
27+
}
28+
// Release the lock of Account 'to'
29+
System.out.println(amount + "is transfered from " + from + " to " + to);
30+
}
2931
}

0 commit comments

Comments
 (0)