Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find if a thread has finished execution?  RSS feed

 
G Priya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!