Skip to content

Commit 51b9870

Browse files
committed
Thread Deadlock
1 parent c938c00 commit 51b9870

6 files changed

Lines changed: 136 additions & 0 deletions

File tree

threaddeadlock/Account.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Account {
2+
private final String name;
3+
private double balance;
4+
5+
public Account(String name) {
6+
this.name = name;
7+
}
8+
9+
public void withdraw(double amount) {
10+
this.balance -= amount;
11+
}
12+
13+
public void deposit(double amount) {
14+
this.balance += amount;
15+
}
16+
17+
public double getBalance() {
18+
return this.balance;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return name;
24+
}
25+
}

threaddeadlock/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
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);
7+
8+
Transaction t1 = new Transaction("T01", accA, accB, 100.00);
9+
Transaction t2 = new Transaction("T02", accB, accA, 500.00);
10+
11+
t1.start();
12+
t2.start();
13+
}
14+
}

threaddeadlock/Transaction.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
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;
6+
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+
}
13+
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+
}
29+
}

threaddeadlock/fixed/Account.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Account {
2+
private final String name;
3+
private double balance;
4+
5+
public Account(String name) {
6+
this.name = name;
7+
}
8+
9+
public void withdraw(double amount) {
10+
this.balance -= amount;
11+
}
12+
13+
public void deposit(double amount) {
14+
this.balance += amount;
15+
}
16+
17+
public double getBalance() {
18+
return this.balance;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return name;
24+
}
25+
}

threaddeadlock/fixed/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
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);
7+
8+
Transaction t1 = new Transaction("T01", accA, accB, 100.00);
9+
Transaction t2 = new Transaction("T02", accB, accA, 500.00);
10+
11+
t1.start();
12+
t2.start();
13+
}
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
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;
6+
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+
}
13+
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+
}
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+
}
29+
}

0 commit comments

Comments
 (0)