-------
Android training and Java training. We look forward to communicating with you! ----------
Simulate the ATM deposit and money withdrawal process. There are two subjects: One payer and the other payer. Assume that the depositor and the other payer can access the money in turn, save money-withdraw money-save money ......; Knowledge used: thread collaboration, the wait () method of the object class and the notify () method; key point: the wait method and the notify method must be written in the synchronization code and called by the synchronization monitor, the synchronization code can be a synchronous method or a synchronous code block. When written in the synchronous code block, the synchronization monitor is the current object this, so it can be called directly. The key code is as follows:
Public synchronized void getmoney (float money) {If (FLAG) {This. balance-= money; system. out. println ("money Acquisition successful, balance:" + this. balance); flag = false; this. policyall ();} else {try {system. out. println ("I just got the money, please wait! "); This. Wait ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}
The Code is as follows:
Package c16thread;/*** program description: A deposit thread and a money-taking thread. There is such a hypothesis that two threads keep saving and taking money, but they cannot do it twice, after saving the money, you must wait for the money * thread to execute to save the money again. After the money is received, you must wait until the deposit thread executes to get the money again to synchronize the two threads. ** @ Author renyajun **/public class onedepositingtwogetting {public static void main (string [] ARGs) {accounts account = new accounts (); getting4 getperson = new getting4 (account ); depositting depositperson = new depositting (account); thread T1 = new thread (depositperson, "Save Money"); thread t2 = new thread (getperson, "Get money"); t1.start (); t2.start () ;}/// account class, a balance variable and Its getter method, a money Acquisition method, and a deposit method class accounts {// balance F Loat balance = 0; Boolean flag = false; public float getbalance () {return balance;} // method of obtaining money public synchronized void getmoney (float money) {// If (FLAG) {// If (this. balance> 0) {// This. balance-= money; // system. out. println ("money Acquisition successful" + this. balance); // flag = false; // policyall (); //} else {// try {// wait (); //} catch (interruptedexception E) {// todo auto-generated Catch Block // E. printstacktrace ();//}//}//} If (FLAG) {This. balance-= money; system. out. println ("money Acquisition successful, balance:" + this. balance); flag = false; this. policyall ();} else {try {system. out. println ("I just got the money, please wait! "); This. wait ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}// save method public synchronized void depositmoney (float money) {If (! Flag) {This. balance + = money; system. out. println ("saved successfully, balance:" + this. balance); flag = true; policyall ();} elsetry {system. out. println ("just saved money, please wait! "); Wait ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}// the money receiving Thread class getting4 implements runnable {// holds the accounts account variable of the accounts type; // when creating an object, input the variable public getting4 (Accounts account) {This. account = Account ;}@ overridepublic void run () {for (INT I = 0; I <10; I ++) {account. getmoney (800); system. out. println ("----------- the" + I + "times" + thread. currentthread (). getname () + "completed ----------") ;}}// saving Thread class depositting implements runnable {// The accounts account variable holding the accounts type; // when creating an object, input the variable public depositting (Accounts account) {This. account = Account ;}@ overridepublic void run () {for (INT I = 0; I <10; I ++) {account. depositmoney (800); system. out. println ("----------- the" + I + "times" + thread. currentthread (). getname () + "completed ----------");}}}
Execution result: