Any way to Kill a thread?

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

Any way to Kill a thread?

John Alexander-7
I realize that the escape key will stop a macro - but I've recently been
 using some computationally intensive plug-ins which can run for many
minutes (or hours!).  Sometimes, after starting them, I realize either I
have a bad parameter, or I should have preconditioned or cropped the
input image, or the results are not looking right etc etc - but I can't
find anyway of aborting the plug-in - other than killing the ImageJ
application.

If the plug-in does not respond to "closing" nor does it have a "cancel"
option - is there anyway of aborting a thread?


John
Reply | Threaded
Open this post in threaded view
|

Re: Any way to Kill a thread?

dscho
Hi,

On Tue, 15 Jul 2008, John Alexander wrote:

> If the plug-in does not respond to "closing" nor does it have a "cancel"
> option - is there anyway of aborting a thread?

I wrote a really simple plugin to do that some time ago:

-- snip --
import ij.IJ;
import ij.gui.GenericDialog;
import ij.plugin.PlugIn;

public class Thread_Killer implements PlugIn {
        public void run(String arg) {
                ThreadGroup group = Thread.currentThread().getThreadGroup();
                int activeCount = group.activeCount();
                Thread[] threads = new Thread[activeCount];
                group.enumerate(threads);
                int j = 0;
                for (int i = 0; i < activeCount; i++) {
                        String name = threads[i].getName();
                        if (threads[i] == Thread.currentThread() ||
                                        name.startsWith("AWT-") ||
                                        name.equals("Java2D Disposer") ||
                                        name.equals("SocketListener") ||
                                        name.equals("DestroyJavaVM") ||
                                        name.equals("TimerQueue"))
                                continue;
                        if (j < i)
                                threads[j] = threads[i];
                        System.err.println("nr " + i + ": " + threads[i].getName());
                        j++;
                }
                activeCount = j;
                if (activeCount == 0) {
                        IJ.showMessage("No threads to kill.");
                        return;
                }

                String[] names = new String[activeCount];
                for (int i = 0; i < activeCount; i++)
                        names[i] = threads[i].getName();
                GenericDialog gd = new GenericDialog("Thread to kill");
                gd.addChoice("thread", names, names[0]);
                gd.showDialog();
                if (gd.wasCanceled())
                        return;
                int threadIndex = gd.getNextChoiceIndex();
                threads[threadIndex].stop();
        }
}
-- snap --

If you have a JDK (as opposed to just a JRE) you can make a new plugin
without restarting ImageJ, paste the code, compile and run it.

May it help,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: Any way to Kill a thread?

Gabriel Landini
On Wednesday 16 July 2008 12:49:58 Johannes Schindelin wrote:
> If you have a JDK (as opposed to just a JRE) you can make a new plugin
> without restarting ImageJ, paste the code, compile and run it.

It gives a warning :

Thread_Killer.java:41: warning: [deprecation] stop() in java.lang.Thread has
been deprecated
                threads[threadIndex].stop();

Cheers,

G.
Reply | Threaded
Open this post in threaded view
|

Re: Any way to Kill a thread?

dscho
Dear Gabriel,

On Wed, 16 Jul 2008, Gabriel Landini wrote:

> On Wednesday 16 July 2008 12:49:58 Johannes Schindelin wrote:
> > If you have a JDK (as opposed to just a JRE) you can make a new plugin
> > without restarting ImageJ, paste the code, compile and run it.
>
> It gives a warning :
>
> Thread_Killer.java:41: warning: [deprecation] stop() in java.lang.Thread has
> been deprecated
>                 threads[threadIndex].stop();

Yes, I know.  However, the description of the deprecation is less than
satisfying: Sun asks you to modify the _to-be-killed_ thread, which is not
an option here.

Ciao,
Dscho