27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Threads<br />

Joe<br />

Q: When will a Thread Object be garbage collected?<br />

I would like to ask a question about garbage collection of Thread Object.<br />

When will a Thread Object be garbage collected?<br />

When the reference count to it becomes zero, or when it enters the "Dead" state, i.e.<br />

the run() member function terminates?<br />

Answer: Since Thread is also an Object, it will only garbage collected when the<br />

reference count is zero.<br />

You may think it is quite non-sense. the thread is useless when it enter "dead" state.<br />

why not garbage collect it? That's because the thread object itself may contain some<br />

other useful information even the thread dead , e.g. the result of the execution of the<br />

thread. Thus, it is not sensible to do garbage collect when the reference count is not<br />

zero.<br />

Hope this answer your question.<br />

--<br />

Anthony<br />

Q: I would like to ask a question about garbage collection of Thread Object.<br />

When will a Thread Object be garbaged collected?<br />

When the reference count to it becomes zero, or when it enters the "Dead" state, i.e.<br />

the run() member function terminates?<br />

Answer, part1: Since Thread is also an Object, it will only garbage collected when<br />

the reference count is zero. You may think it is quite non-sense. the thread is<br />

useless when it enter "dead" state. why not garbage collect it?<br />

That's because the thread object itself may contain some other useful information<br />

even the thread dead , e.g. the result of the execution of the thread. Thus, it is not<br />

sensible to do garbage collect when the reference count is not zero.<br />

--<br />

Anthony<br />

P.S. Important ad! Except when object A holds a reference only to object B and<br />

object B holds a reference only to object A. Both reference counts are non-zero, but<br />

both objects are eligible for garbage collection. Which is why few, if any, modern<br />

VMs use reference counting to determine eligibility for garbage collection.<br />

Jim<br />

Answer, Part 2: You can consider a Thread object as a normal Object for garbage<br />

collection purposes if you consider this one rule: A running thread will not be garbage<br />

collected.<br />

That is, a normal running thread is a root object, so the Garbage Collector will not<br />

attempt to mark it for collection. When the thread is not running, though, normal<br />

Garbage Collection rules apply (i.e. total # references == 0 then collect).<br />

To get a thread with different behavior,<br />

see the Thread.setDaemon(boolean bState) method.<br />

--Brian<br />

file:///F|/350_t/350_tips/threads.htm (3 of 4) [2002-02-27 21:19:25]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!