• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find if a thread has finished execution?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic