File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Threads and garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Threads and garbage collection" Watch "Threads and garbage collection" New topic
Author

Threads and garbage collection

Micah Wedemeyer
Ranch Hand

Joined: Jun 11, 2001
Posts: 68
If I create a Thread and do not maintain a reference to it like:
(new Thread()).start();
then what happens when that Thread's start() method returns? Will it get marked for garbage collection?
Thanks,
Micah
Thomas Smets
Ranch Hand

Joined: Dec 11, 2001
Posts: 111
First of all the code is incorrect

If I create a Thread and do not maintain a reference to it like:
(new Thread()).start();



Then no the Thread you instanciate is not ready for GC as soon as you quit the line invoking the start () but when the run method of the Runnable. The Thread instance & all the object it may refere may be marked for GC if they are not referenced by anything anymore...
Thomas,


Thomas Smets
Just another developper
Micah Wedemeyer
Ranch Hand

Joined: Jun 11, 2001
Posts: 68
I knew that Thread would do nothing, it was just an example. My actual object in my application is simply a subclass of Thread.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Basically, objects may be garbage collected if and only if they are not reachable from any living threads. This means that threads are the starting points for defining what is reachable, and what isn't - and even if no other threads hold any references to a Thread object, once start() has been called it cannot be garbage collected until its run() method completes (whether it completes normally or abruptly).


"I'm not back." - Bill Harding, Twister
 
 
subject: Threads and garbage collection
 
Threads others viewed
doubt on garbage collection
GC and finalize()
what is daemon threads
daemon thread & user thread
why finalize() method is not called directly?
developer file tools