On Apr 3, 2008, at 12:31 PM, John Cowan wrote:

> That reminds me of an old question of mine.  In the case where a
> thread has been created from an object whose class is a subclass of
> Thread, is Thread.currentThread() guaranteed to give you back the same
> object?

Yes.  You can then cast it to the subclass and go on from there.

> If so, that provides a different approach
> to thread-local state, where the state is held in the instance
> variables of MyThread objects, and the JVM is used to thread them
> through :-) the code until the point where they're needed.

That's how Java thread locals are implemented in the JVM.

You can only use this approach if (a) you control the creation of the  
thread and can specify your own subclass for it, or (b) you can edit  
the rt.jar code for java.lang.Thread.  Plan (c) is to make a  
ThreadLocal.

You might be interested to know that Hotspot intrinsifies  
Thread.currentThread to a couple of instructions; it's cheap.  We did  
that to make things like thread locals efficient.

Best,
-- John

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to jvm-languages@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to