Assuming that the system now has two threads, the two threads represent depositors and the money-seekers-now assume that the system has a special requirement, and the system requires depositors and money-seekers to keep repeating their deposits and taking money. It is also required that whenever the depositor deposits the money into the designated account, the person who takes the money will withdraw the money immediately. Depositors are not allowed to save twice a row, nor are they allowed to withdraw money twice in succession.
To achieve this, you can use the Wait (), notify (), Notifyall () 3 methods provided by the object class, and these 3 methods do not belong to the thread class but to the object class. However, these 3 methods must be called by the synchronization monitor object, which can be divided into the following two scenarios:
1, for the synchronization method using synchronized adornment, because the default instance of the class is (this) is the synchronization monitor. So you can call these 3 methods directly in a synchronous method.
2. For a synchronous block of code that uses synchronized adornments, the synchronization monitor is an object in the synchronized bracket, so you must use that object to call these 3 methods.
The explanations for these 3 methods are as follows:
Wait (): causes the current thread to wait until another thread calls the synchronization Monitor's notify () method or the Notifyall () method to wake the thread. The wait () method has three forms-wait with no time parameter (waits, knows other thread notifications), wait with millisecond parameter, and wait with millisecond, minute parameter (both methods are automatically awakened after a specified time) The current thread that calls the wait method frees the lock on the synchronization monitor.
notify (): wakes up a single thread waiting on the secondary synchronization monitor. If all the threads are waiting on this synchronization monitor, they will choose to wake up one of the threads. The selection is arbitrary. The wake-up thread can only be executed if the front-thread discards the lock on the synchronization monitor (using the Wait method).
notifyall (): wakes up all the threads waiting on this synchronization monitor. The wake-up thread can only be executed if the front-thread discards the lock on the synchronization monitor.
1 Public classdrawtest{2 Public Static voidMain (string[] args) {3Account A=NewAccount ("Liu Teng", 10000);4 NewDrawthread (a,100). Start ();5 NewInthread (a,50). Start ();6 NewInthread (a,60). Start ();7 NewInthread (a,70). Start ();8 }9 }Ten //Account Class One classaccount{ A PrivateString name; - Private DoubleMoney ; - Private Booleanflag=false; the - PublicAccount () {} - PublicAccount (String name,DoubleMoney ) { - This. name=name; + This. money=Money ; - } + Public DoubleGetmoney () { A returnMoney ; at } - Public synchronized voidDrawDoubleDrawmoney) { - Try{ - //if flag is false, it means that no one is saving money in it, so let the thread wait. - if(!flag) { - wait (); in}Else{ -System.out.println ("Withdraw money for:" +drawmoney+ "Remaining Amount:" + (Getmoney ()-Drawmoney)); toflag=false; + Notifyall (); - } the}Catch(Exception e) {System.out.println (e);} * } $ Public synchronized voidDepositDoubleInmoney) {Panax Notoginseng Try{ - if(!flag) { theSystem.out.println ("Deposit:" +inmoney+ "the current amount of money is:" + (Getmoney () +Inmoney)); +flag=true; A Notifyall (); the}Else{ + wait (); - } $}Catch(Exception e) {} $ } - } - //Take money thread the classDrawthreadextendsthread{ - PrivateAccount account ;Wuyi Private DoubleDrawmoney; the - PublicDrawthread (Account account,DoubleDrawmoney) { Wu This. account=Account ; - This. drawmoney=Drawmoney; About } $ Public voidrun () { - for(inti=0;i<100;i++){ - Account.draw (Drawmoney); - } A } + } the classInthreadextendsthread{ - PrivateAccount account ; $ Private DoubleInmoney; the the PublicInthread (Account account,DoubleInmoney) { the This. account=Account ; the This. inmoney=Inmoney; - } in Public voidrun () { the for(intk=0;k<100;k++){ the account.deposit (Inmoney); About } the } the}
Java Thread Communication (traditional threading)