[ImageJ-devel] ImageJ2 plugin thread question

Johannes Schindelin Johannes.Schindelin at gmx.de
Thu Feb 13 09:43:45 CST 2014


Hi Yili,

On Thu, 13 Feb 2014, Yili Zhao wrote:

>   if I want to develop a plugin for ImageJ2, there are basically two kinds
> of plugin:
> 
>   1. first kind plugin does not need GUI;
> 
>   2. second kind plugin does need GUI.

There are many more, in particular, you need to separate 2) into at least
3 subcategories: plugins that need a simple GUI enquiring parameters,
plugins that need a simple GUI with minor interactivity (such as
interpreting a "maintain aspect-ratio" checkbox to adjust the width if the
user edits the height, and vice versa) and fully interactive GUIs (think
Trainable Segmentation).

In general, all of these mentioned plugins are actually *commands*: they
*run*.

There are other plugins (even in ImageJ 1.x, where the term "plugin" gets
really, really blurred): tools, file formats for I/O, scripting languages,
complete user interfaces, etc.

>   Suppose this plugin will perform a long time-consumed task, so I want to
> ask that:
> 
>   1. will ImageJ2 run its plugin in a new thread? Because in Swing toolkit,
> all UI events should be processed in UI thread or the main thread, and
> there is only One UI thread, so it is a single thread model.
> Running a plugin in a new thread can guarantee the plugin will not block
> ImageJ2's GUI.

The idea in ImageJ2 is to avoid Swing-specific stuff (otherwise you doom
yourself to a very limited, narrow mindset: one user, one machine, one
desktop, something that is already not true at all).

So what you would do is subscribe to the event service and react to those
events.

>   2. If this plugin need its own GUI, but this plugin is running in a
> thread which is not the UI thread (event dispatch thread), and this
> plugin's GUI should be created in UI thread for thread safe, how can this
> be done?

The same way as it is done with Swing: you subscribe to events and react
on them. If the reaction takes a long time, you will have to spawn off a
new thread to avoid blocking the main event thread.

>   In order to avoid blocking the ImageJ2' user interface, I plan to do like
> this:
> 
>   1. execute the "long time-consumed task" in a separate worker thread;

Good idea.

>   2. In the task running process, I want to report the running progress to
> the ImageJ2 main frame, so that the main frame can display a progress
> information to let user know the current status;

Good. You will need to add a @Parameter field of type StatusService to do
that.

>   3. When the task completed, it can notify the main frame, and the main
> frame can response to this event.

If you refer to the main window as the main frame, there is no need. If
you refer to your plugin's main frame, you'll just have to dispose() of
it.

>   How can I implement this asynchronous task model in ImageJ2? (I think
> maybe I need some helper classes like  "SwingWorker" in JDK 6)

As I mentioned earlier, restricting yourself to Swing is bound to get you
into such a lot of trouble it is simply not worth it.

Better look at the ThreadService:

	http://jenkins.imagej.net/view/SciJava/job/SciJava-common-javadoc/javadoc/org/scijava/thread/ThreadService.html

Ciao,
Johannes



More information about the ImageJ-devel mailing list