java - Semaphore synchronized wait() and notify() -


I have the code: it is getting knowledge of Sikandra before code thread A b is waiting (calling) Thread sleeping), then inform, why the code has been synchronized here? If not, this unlawful Monteroaf XXXXXXXX exception.

  public class thread A {public static zero main (string [] args) {thread bb = new thread b (); B.start (); Synchronize (B) {try {System.out.println ("Waiting for B to be completed ..."); B.wait (); } Grip (Interrupted e) e.printStackTrace (); } System.out.println ("Total:" + b.total); }}} Class ThreadB thread {int total; @ Override Public Wide Run () {synchronize (this) {for (int i = 0; i & lt; 100; i ++) {total + = i; }             Notify(); }}}  

Edit: The answer is that the synchronization informs the current thread of the owner of the saccharoy () documents that show.

You should only use the same as the last object monitor. This will save you some bugs.

If your monitor is not created last, then it is always a chance that this will be set to refer to a new object and such a case code can be synchronized, in fact, in parallel Read more to run.

How it happened:

A. Consumer:

Fixed Last Item Monitor = New Object (); // & lt; --------------- Fixed volatile boolean complete = false; Public square thread A {public static zero main (string [] args) {thread bb = new thread b (); B.start (); Synchronize {while (complete!) {Monitor.wait (); }} // when here: the creator did & lt; -----------------}}

B Creator:

Extensions of the square ThreadB thread {int total; @ Override Public Wide Run (for (for int i = 0; i & lt; 100; i ++) {// & lt; ------------ Total + = i synchronized Do not need to;} full = true; & lt; --------- icon maker as full (monitor) {monitor.notify ();}}}

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -