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 )
{ //....
}
}
}
public class ServerTest implements Runnable
{
public static void main(String[] args)
{
new Thread( new ServerTest() ).start();
}
public void run()
{
while( true )
{ //....
}
}
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks