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
posted
0
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
posted
0
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
posted
0
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).