Originally posted by Priya India:
Hi, Am new to multi-threaded programming. I have a question: I start a new thread from the main thread. This new thread has to update a swing component & thats it. How do I know when the new thread has finished its execution? Also, I need to kill the thread once it finishes its execution?..or will Java's GC take care of it? If I need to kill it from the code, how do I do it? I see that the destry() method of Thread class is deprecated.
Thanks.
To check if a thread has completed, you may use the isAlive() method after you have started the thread. Or you can use the join() method to wait for the thread to finish.
The thread doesn't have to be killed after it finishes. The GC will take care of the Thread object, sometime after you dereference it.
But...
Swing is *not* thread safe, you are not allowed to call methods of Swing components (from any thread) after they have been started. They must be done from the event dispatching thread... to do that you can use the invokeLater() or invokeAndWait() methods of the SwingUtilites class.
Henry