Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is with auto-boxing. When you change <code>flag</code> from true to false or vice versa, you are actually getting an entirely new <code>Boolean</code> object. That is, this line:</p> <pre><code>flag = false; </code></pre> <p>Is equivalent to:</p> <pre><code>flag = new Boolean(false); </code></pre> <p>Once that happens your two threads are then referring to two different <code>Boolean</code> objects, so their flags end up un-synchronized and neither thread is able to signal the other to wake up. When <code>OddThread</code> changes the flag <code>EvenThread</code> still has the old flag object so it doesn't see the new value.</p> <p>Because a <code>Boolean</code> object is immutable you'll need to change your flag to use some other mutable object which can change values in place without creating new objects. That, or have both classes refer to a common (perhaps global) variable.</p> <p>As @erickson suggests you could use <code>AtomicBoolean</code> which is mutable. Another kludgy way to do it would be to change <code>flag</code> to:</p> <pre><code>boolean[] flag = new boolean[1]; </code></pre> <p>And then use <code>flag[0]</code> every where. Both threads would then be able to change <code>flag[0]</code> while always referencing the same <code>boolean[]</code> array object. You wouldn't have the auto-boxing problem.</p> <p>...</p> <p>Also, it is a good idea to wrap any call to <code>wait()</code> in a loop. A <code>wait()</code> can be subject to spurious wakeups where the call returns even though nobody has actually called <code>notify()</code>. To workaround that you should always check your guarding condition after waking up to make sure the wakeup isn't spurious.</p> <pre><code>while (flag == true) { lock.wait(); } </code></pre> <hr> <h2>Update</h2> <blockquote> <p>I have made the changes based on your suggestions above; but i am not getting the expected output. I will paste the modified code above. Here is the output i am getting 1 2 4 3 5 6 8 7 9 10 11 13 12 15 17 14....</p> </blockquote> <p>When you end up waiting, once you are woken up you don't toggle <code>flag</code> and notify the other thread. I advise reorganizing your code a bit so it looks like "wait; print; notify". Something like:</p> <pre><code>synchronized (lock) { while (flagObj.flag == false) { lock.wait(); } System.out.println(i); flagObj.flag = false; lock.notify(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload