I load data in a background mode with a swing worker thread. A simplified version of the routine is below. The important part is work2 holds the object until the thread is done and then work2 gets a null value.
What I might expect is the garbage collection to remove the thread, but I see in the debugger that SwingWorker-pool-1-thread-1-running. From my point of view, I have no more use for that thread, since the loading is done.
It could well be that I am best off simply to ignore the fact that the thread is still running, but I would like to ask anyway. Is it desirable to explicitly kill the thread? I haven't yet looked for the explicit command to do this simply because I am used to setting the object to null and as soon as there are no more references, Java kills things for me.
So what is the best advice? Kill it or ignore it?
Thanks, Ilan
protected void LoadData(PetCtFrame par1, ArrayList<ImagePlus> imgList, ArrayList<Integer>seriesType) {
work2 = new bkgdLoadData();
work2.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if( propertyName.equals("state")) {
SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
if( state == SwingWorker.StateValue.DONE) {
work2 = null;
}
}
}
});
work2.execute();
}
Once a SwingWorker
is first used, its pool is initialized and usually there is no need to manage it.
As for your mention of garbage collection, this has nothing to do with thread management. The Thread
object is not a thread, it is just a handle object which allows you to start and manage the lifecycle of a thread of execution. As soon as the underlying thread is started, the Thread
instance is statically reachable as Thread.currentThread()
, therefore the instance will not be subject to garbage collection as long as the thread is alive.