IJ.wait() and threads

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

IJ.wait() and threads

Tony Shepherd
Dear users,
I am developing a plugin which needs to use the 'wait' command, to respond
to users interacting with the image window (using roi tools).

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.

Can anyone shed light on this?

Thanks:.....

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;
       }


  // WORKS INSIDE THE BUTTON BELOW------------

  myButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {

     Thread t1 = new Thread(new Runnable() {
     public void run() {

       while(!finished)
       {
         IJ.wait(100);
         if(interaction) do something;
       }

      }// thread run          
      });// new thread
      t1.start();

   }
 }


}
}
Reply | Threaded
Open this post in threaded view
|

Re: IJ.wait() and threads

dscho
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
Reply | Threaded
Open this post in threaded view
|

Re: IJ.wait() and threads

Michael Schmid
In reply to this post by Tony Shepherd
Hi Tony,

besides using wait() and notify() as mentioned by Johannes, you could  
also have a look at ij.gui.WaitForUserDialog, maybe it is suited for  
your needs.

Michael
________________________________________________________________

On 4 Jun 2009, at 08:00, Tony Shepherd wrote:

> Dear users,
> I am developing a plugin which needs to use the 'wait' command, to  
> respond
> to users interacting with the image window (using roi tools).
>
> 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.
>
> Can anyone shed light on this?
>
> Thanks:.....
>
> 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;
>        }
>
>
>   // WORKS INSIDE THE BUTTON BELOW------------
>
>   myButton.addActionListener(new ActionListener() {
>   public void actionPerformed(ActionEvent e) {
>
>      Thread t1 = new Thread(new Runnable() {
>      public void run() {
>
>        while(!finished)
>        {
>          IJ.wait(100);
>          if(interaction) do something;
>        }
>
>       }// thread run
>       });// new thread
>       t1.start();
>
>    }
>  }
>
>
> }
> }