Recently in the study of wait and notify method, found that there is a place to pay attention to, but the Internet is not very clear about the place, is the classic producer and consumption patterns, using wait and notify implementation, to determine whether the list is empty why use while instead of using if? This is because when the thread is waiting and then wakes up, it is executed from the wait, not from the beginning, so if it is awakened then it will not be judged if the condition in the IF, but continues to execute, if the list just adds a data, And there are two of consumers awakened, there will be overflow problem, because will not be judged whether the size is ==0 directly execute remove. However, if you use the while, continue execution from wait, and then return to the conditional judgment of execution while size>0 the remove operation, so this must use while instead of using if as a judgment.
The following code is a simple practice, refer to: http://www.cnblogs.com/hapjin/p/5492645.html (made the appropriate adjustments)the Add class is responsible for adding data:
public class Add { private String lock; Public Add (String Lock) { super (); This.lock = lock; } public void Add () { synchronized (lock) { ValueObject.list.add ("AnyString"); Lock.notifyall ();}}} public class Threadadd extends Thread { private Add p; Public Threadadd (Add p) { super (); THIS.P = P; } @Override public Void Run () { p.add (); }}
Subtract class, responsible for deleting data
public class Subtract {private String lock; Public Subtract (String Lock) {super (); This.lock = lock; } public boolean check () {System.out.println ("check"); return true; public void Subtract () {try {synchronized (lock) {if (check () && Valueobje Ct.list.size () = = 0) {//change this if to while to ensure that there is no out-of-bounds exception!!!! SYSTEM.OUT.PRINTLN ("Wait begin Threadname=" + thread.currentthread (). GetName ()); Lock.wait (); SYSTEM.OUT.PRINTLN ("Wait end threadname=" + Thread.CurrentThread (). GetName ()); } ValueObject.list.remove (0); System.out.println ("list size=" + ValueObject.list.size ()); }} catch (Interruptedexception e) {e.printstacktrace (); }}}public class Threadsubtract extends Thread {private Subtract R; Public ThreadsubtRact (Subtract R) {super (); THIS.R = R; } @Override public void Run () {r.subtract (); }}
encapsulated List queue:
Public class Valueobject { publicstaticnew ArrayList ();}
Test class:
Public classRun { Public Static voidMain (string[] args)throwsinterruptedexception {String lock=NewString (""); Add Add=NewAdd (lock); Subtract Subtract=NewSubtract (lock); Threadsubtract Subtract1thread=Newthreadsubtract (subtract); Subtract1thread.setname ("Subtract1thread"); Subtract1thread.start (); Threadsubtract Subtract2thread=Newthreadsubtract (subtract); Subtract2thread.setname ("Subtract2thread"); Subtract2thread.start (); Thread.Sleep (1000); Threadadd Addthread=NewThreadadd (add); Addthread.setname ("Addthread"); Addthread.start (); }}
From for notes (Wiz)
Test a condition change in multiple threads in Java with If or while?