3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2008 Albert Cardona.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
23 package ini
.trakem2
.utils
;
25 import java
.util
.ArrayList
;
26 import javax
.swing
.SwingUtilities
;
28 public class Dispatcher
extends Thread
{
29 private final class Task
{
32 Task(Runnable run
, boolean swing
) {
37 private final ArrayList
<Task
> tasks
= new ArrayList
<Task
>();
38 private final Lock lock
= new Lock();
39 private boolean go
= true;
41 public Dispatcher(String tag
) {
42 super("T2-Dispatcher" + (null != tag ?
" " + tag
: ""));
43 setPriority(Thread
.NORM_PRIORITY
);
52 synchronized (this) { notify(); }
54 public boolean isQuit() {
58 private boolean accept
= true;
60 public void quitWhenDone() {
65 /** Submits the task for execution and returns immediately. */
66 public void exec(final Runnable run
) { exec(run
, false); }
67 public void execSwing(final Runnable run
) { exec(run
, true); }
68 public void exec(final Runnable run
, final boolean swing
) {
70 Utils
.log2("Dispatcher: NOT accepting more tasks!");
75 tasks
.add(new Task(run
, swing
));
78 synchronized (this) { notify(); }
80 /** Executes one task at a time, in the same order in which they've been received. */
82 while (go
|| (!accept
&& tasks
.size() > 0)) {
84 synchronized (this) { wait(); }
86 while (tasks
.size() > 0) {
90 if (tasks
.size() > 0) {
91 task
= tasks
.remove(0);
95 if (null == task
) continue;
97 if (task
.swing
) SwingUtilities
.invokeAndWait(task
.run
);
99 } catch (Throwable t
) { IJError
.print(t
); }
101 } catch (Throwable e
) { IJError
.print(e
); }