[PATCH]: natThread.cc: Destroy join sync objects in finalizer

Bryce McKinlay mckinlay@redhat.com
Mon Sep 13 17:04:00 GMT 2004


Tom Tromey wrote:

>Hans> In this case, is it possible to invoke the cleanup code from
>Hans> a java.lang.Thread finalizer?  Is java.lang.Thread allowed to have
>Hans> a finalizer?
>
>It can have a finalizer, but user code can override it.  So, there's
>no guarantee it will be run.
>
>A PhantomReference seems to be the most reliable way to do cleanup in
>this situation.  We could point it at the Thread object, then do
>cleanup from a watching ReferenceQueue.  This way the cleanup code
>will only be called once the Thread (and its native data) are
>irrevocably dead.
>  
>
>We could also just clean up the native state when the thread dies.
>That is probably even better.
>  
>
Yes, this would be cleaner, but we'd need to be careful that 
Thread.join() etc don't try to access the native state after it has been 
freed. To avoid race problems here we could perhaps register the 
finializer on the native state (natThread) itself rather than the 
java.lang.Thread, and atomically release it (set "thread->nt" to null) 
when the thread dies.

Bryce



More information about the Java-patches mailing list