[ImageJ-devel] ImageJ2 plugin thread question

Curtis Rueden ctrueden at wisc.edu
Thu Feb 13 16:53:09 CST 2014


Hi Yili,

> will ImageJ2 run its plugin in a new thread?

Yes, ImageJ2 launches plugins in their thread. See:
http://jenkins.imagej.net/job/ImageJ-daily/javadoc/imagej/module/ModuleService.html#run(imagej.module.ModuleInfo,
boolean, java.lang.Object...)

ImageJ modules are typically fired using these run methods, which spawn a
new thread using the ThreadService -- well, it's out of a ThreadPool, so in
theory you could hit a limit if you run tons of modules at the same time.

Note that the methods return a Future, so that you can block till execution
is complete *if you want* but you are not forced to do so.

> Suppose this plugin will perform a long time-consumed task

If you write a command whose sole job is to perform some long-running task,
just implement the task in the run method. You do not have to worry about
threading unless you want the execution itself to be done using multiple
threads. (In the future we plan to provide more transparent
multi-threading, but right now modules execute on a single new thread.)

> 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?

You do not need to create Swing UI component on the EDT. You can do it in
any thread. It is only *after* you pack() and setVisible(true) that you
need to be concerned about the EDT, and even then, if all you are doing is
listening for GUI events (e.g., actionPerformed callbacks) then those
already happen on the EDT so you don't need to worry.

In short, in 99% of cases, you actually do not need to use
EventQueue.invokeLater and similar. So even if you write an ImageJ command
that spawns a Swing UI, you may not need this method.

As Johannes says, if you want to stay general, look at the ImageJ
ThreadService. It has UI-agnostic entry points into concepts like the EDT,
without explicitly tying them to AWT/Swing in particular.

> In the task running process, I want to report the running progress to
> the ImageJ2 main frame

http://jenkins.imagej.net/job/SciJava-common-javadoc/javadoc/org/scijava/app/StatusService.html

The ImageJ UI (be it a Swing one, or other one) will take care of rendering
your status messages and progress updates to the status bar or however it
sees fit.

Regards,
Curtis


On Thu, Feb 13, 2014 at 8:19 AM, Yili Zhao <panovr at gmail.com> wrote:

> Hi,
>   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.
>
>   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.
>
>   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?
>
>   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;
>
>   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;
>
>   3. When the task completed, it can notify the main frame, and the main
> frame can response to this event.
>
>   How can I implement this asynchronous task model in ImageJ2? (I think
> maybe I need some helper classes like  "SwingWorker" in JDK 6)
>
>   Thanks!
>
>
> --
> Yili Zhao
>
> _______________________________________________
> ImageJ-devel mailing list
> ImageJ-devel at imagej.net
> http://imagej.net/mailman/listinfo/imagej-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://imagej.net/pipermail/imagej-devel/attachments/20140213/63d81f6f/attachment.html>


More information about the ImageJ-devel mailing list