Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>See <a href="https://stackoverflow.com/questions/12023056/java-wait-and-notify-in-a-non-synchronized-block#comment16045203_12023056">my comment</a>. Since <code>IllegalMonitorStateException</code> is never thrown, we know that <code>wait</code> is never being called.</p> <p>Notice you have two instances of <code>counthrd</code>...</p> <pre><code>counthrd cnt1 = new counthrd(); counthrd cnthrd1 = new counthrd("thrd 1", cnt1); </code></pre> <p>See which instance you're calling <code>suspendme</code> and <code>resumeme</code> on?</p> <pre><code>Thread.sleep(1000); System.out.println("going to wait mode"); cnt1.suspendme(); Thread.sleep(1000); System.out.println("resuming"); cnt1.resumeme(); Thread.sleep(1000); </code></pre> <p><code>cnt1</code> is initialized using your no-arg constructor, seen here:</p> <pre><code>counthrd() { } </code></pre> <p>The point is that <code>cnt1</code> never actually starts its own thread. It never does anything, really. <code>cnthrd1</code> is the one that starts a thread, as seen here:</p> <pre><code>counthrd(String s, counthrd cnt1) { thrd = new Thread(this, s); this.cnt1 = cnt1; thrd.start(); x = s; } </code></pre> <p>The point to make is that <code>suspended</code> is an instance field, and not shared between <code>cnt1</code> and <code>cnthrd1</code>. Modifying <code>cnt1.suspended</code> will not cause <code>cnthrd1</code> to go into "wait mode". <code>wait</code> is never called, and thus the exception is never thrown.</p> <p>To demonstrate, try calling <code>suspendme</code> and <code>resumeme</code> on <code>cnthrd1</code>, instead... :-)</p> <pre><code>C:\dev\scrap&gt;javac counter.java C:\dev\scrap&gt;java counter Starting thrd 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 going to wait mode going to wait mode java.lang.IllegalMonitorStateException resuming </code></pre> <hr> <p>That being said, I figured I'd suggest you do some stuff that your code should be doing.</p> <ol> <li>Declare <code>suspended</code> as <code>volatile</code>. Without some explicit memory ordering guarantees, there's no guarantee when or even <em>if</em> <code>cnthrd1</code> reads the updated value of <code>suspended</code>.</li> <li>Ditch the <code>cnt1</code> field and instance; there's no reason for them. Get rid of that empty constructor, too.</li> <li><code>Thread.currentThread</code> is a static method; you don't need to use an instance for it. That all aside, <code>thrd</code> is guaranteed to equal <code>Thread.currentThread</code> here.</li> <li><code>counthrd.x</code> is equal to <code>thrd.getName</code>; why not just use <code>x</code> instead?</li> <li>Use some better, more descriptive names. For example, instead of <code>x</code>, why not <code>name</code>? Instead of <code>thrd</code>, why not <code>thread</code>? Instead of <code>counthrd</code>, why not <code>CountingThread</code>?</li> <li>You only need to call <code>notify</code> in <code>resumeme</code>, not <code>suspendme</code>. (in fact, calling <code>notify</code> in <code>suspendme</code> could accidentally trigger an <code>InterruptedException</code> if the thread is sleeping i.e. when <code>(i % 10) == 0</code>)</li> <li>You also don't want <code>notify</code> in the <code>while (suspended)</code> loop. Your <code>while</code> loop can actually be turned into an <code>if</code> statement, too, now.</li> <li>As previously stated, you need <code>synchronized (this)</code> around your code that calls <code>while</code>.</li> <li>Avoid doing real logic in the constructor, e.g. <code>thrd.start()</code>. </li> <li><code>suspend</code> doesn't need to be <code>synchronized</code>. <code>resume</code> doesn't need to be <code>synchronized</code>, either; only the <code>wait</code> and <code>notify</code> calls require it.</li> </ol> <p>You can find a modified version of your example that works properly <a href="http://ideone.com/LLjey" rel="nofollow noreferrer">here</a>.</p>
    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