Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since your program is a GUI program (I'm gathering by the <code>repaint()</code> call), it is inherently multi-threaded, even if you don't know it. (If not, it will behave very badly.)</p> <p>If you are not using Threads, then you cannot use wait/notify or any other kind of synchronization, since there aren't two threads to synchronize. But you don't have to explicitly use Threads, necessarily, in a GUI program to end up using Threads. </p> <p>Note that the Java compiler won't warn you if you use methods relying on Threads but don't actually use Threads in any way. </p> <p>You have one of the following problems:</p> <p>1) You are using Threads without knowing about it and you are using two different monitor objects. Thus, when you call <code>notify()</code>, it is notifying the monitor for that object, but not for the first object where you are calling <code>wait()</code>. There are many possible solutions. One of the easiest is to use the JDK 5 concurrency utilities to do this, which are MUCH nicer than the built-in base monitor wait/notify methods. Or,</p> <p>2) You are running in a single Thread and the wait/notify does not good. It just doesn't make sense in a single-Threaded program to wait for another Thread to notify -- there's no other thread that can do so.</p> <p>Assuming that you are actually using more than one Thread, a good way to solve this with Java 5 and later is with a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html" rel="nofollow noreferrer">semaphore</a>, perhaps by, in the class containing <code>mark()</code> and simplifying a bit:</p> <pre><code>private final Semaphore sem = new Semaphore(1); public void mark(int var) { sem.acquire(); //change some stuff repaint(); sem.release(); } waitForSemaphore() { sem.acquire(); } </code></pre> <p>and then in <code>compute</code>, call <code>waitForSemaphore()</code> when you want to wait to be notified by <code>mark()</code>. Because <code>mark()</code> already acquired the semaphore, you'll have to wait for <code>mark()</code> to release the semaphore before compute will be able to get it by calling <code>waitForSemaphore()</code>.</p>
 

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