Login  Register

Re: IJ.wait() and threads

Posted by dscho on Jun 04, 2009; 8:45am
URL: http://imagej.273.s1.nabble.com/IJ-wait-and-threads-tp3692285p3692287.html

Hi,

On Thu, 4 Jun 2009, Tony Shepherd wrote:

> The wait-inside-while-loop method below causes the thing to hang up if
> it is placed inside the main run() class.
>
> However if I put it behind a separate runnable button then it works. The
> problem is then I would start new threads every time the button is
> pressed.
>
> public class pseudo_code implements PlugIn {
>
>   public void run(String s) {
>
>   // DOESNT WORK IF I PUT IT HERE------------
>
>
>        while(!finished)
>        {
>          IJ.wait(100);
>          if(interaction) do something;
>        }

The question is if the variable "finished" is set to true somewhere in the
code _after_ that run() method: if you want to wait for something to
happen in the same thread that this would happen, you have a classical
dead-lock.

In any case, what you want to do can be done more elegantly with

        // wait for something to happen on this object
        wait();

        ...

        // from another thread, make the wait() return
        thatObject.notify();

Both wait() and notify() are methods that are inherited from
java.lang.Object.

Ciao,
Dscho