File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11public 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}
Original file line number Diff line number Diff line change 11public 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}
Original file line number Diff line number Diff line change 11public 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}
Original file line number Diff line number Diff line change 11public 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}
Original file line number Diff line number Diff line change 11public 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}
Original file line number Diff line number Diff line change 11public 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}
You can’t perform that action at this time.
0 commit comments