Here is still the problem of classic account access money, because in learning thread communication, I learned the Java lock mechanism and tried to use the lock await (), signalall (), signalall () the synchronized wait, policy, and policyall mechanisms have been reproduced.
The logic to be implemented is to save the money first, then withdraw the money, and then save the execution order.
This is an Account type: the classic model, or is there an access and money Acquisition Method in it?
1 package treadtest. notify; 2 3 Import Java. util. concurrent. locks. condition; 4 Import Java. util. concurrent. locks. reentrantlock; 5 6 public class account {7 private string age; 8 private double count; 9 10 11 private reentrantlock lock = new reentrantlock (); 12 private condition = lock. newcondition (); 13 14 15 16 17 private Boolean flag = false; // true has saved false not saved 18 19 public account (string Age, double count) {20 this. age = age; 21 this. count = count; 22} 23 24 Public String getage () {25 return age; 26} 27 public void setage (string age) {28 This. age = age; 29} 30 public double getcount () {31 return count; 32} 33 public void setcount (double count) {34 this. count = count; 35} 36 37 38 39 public void draw (double drawcount, int num) {40 lock. lock (); 41 try {42 if (! Flag) {// No Money 43 condition. await (); // similar to synchised's wait () method 44 system. out. println (thread. currentthread (). getname () + "no" + num + "no money to wait"); 45} 46 If (this. count> = drawcount) {47 system. out. println (thread. currentthread (). getname () + "no." + num + "money Acquisition" + drawcount + "RMB"); 48 This. setcount (this. count-drawcount); 49 system. out. println ("remaining" + count); 50 flag = false; 51 condition. signalall (); 52 53} 54 else {55 system. out. println ("Insufficient balance! "); 56} 57} catch (exception e) {58} finally {59 lock. unlock (); 60} 61} 62 63 public void deposit (double depositcount, int num) {64 lock. lock (); 65 try {66 If (FLAG) {// rich 67 condition. await (); // similar to synchised's wait () method 68 system. out. println (thread. currentthread (). getname () + "no." + num + "deposit money waiting"); 69} 70 system. out. println (thread. currentthread (). getname () + "no." + num + "Times saved to" + depositcount + "RMB"); 71 This. setcount (this. count + depositcount); 72 system. out. println ("remaining" + count); 73 flag = true; 74 condition. signalall (); // similar to the synchronized y () method of synchised 75} catch (exception e) {76} finally {77 lock. unlock (); 78} 79 80} 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96}
Deposit Thread class, responsible for saving money
1 package treadTest.notify; 2 3 public class Deposit extends Thread{ 4 private Account account; 5 6 public Deposit(Account account) { 7 this.account = account; 8 } 9 10 11 @Override12 public void run() {13 for(int i=0;i<100;i++){14 account.deposit(100.0,i+1);15 }16 }17 18 }
Multi-thread billing: Money Collection
1 package treadTest.notify; 2 3 public class Draw extends Thread{ 4 private Account account; 5 6 public Draw(Account account) { 7 this.account = account; 8 } 9 10 11 @Override12 public void run() {13 for(int i=0;i<100;i++){14 account.draw(90.0,i+1);15 }16 }17 }
Money access test:
1 package treadTest.notify; 2 3 public class AccountTest { 4 5 6 public static void main(String[] args) { 7 Account account = new Account("lina", 0.0); 8 new Deposit(account).start(); 9 new Draw(account).start();10 }11 12 }
The output is as follows:
Thread-0 1st times saved 100.0 yuan
Remaining 100.0
Thread-1 1st get $90.0
Remaining 10.0
Thread-0 2nd saving money and waiting
Thread-0 2nd times saved 100.0 yuan
Remaining 110.0
Thread-1 2nd get $90.0
................................................
Remaining 990.0
Thread-0 100th saving money and waiting
Thread-0 100th times saved 100.0 yuan
Remaining 1090.0
Thread-1 100th get the money and no money to wait
Thread-1 100th get $90.0
Remaining 1000.0
OK !, I learned a bit.