[concurrency-interest] Soliciting input about removeAllThreadLocals

Joshua Bloch joshua.bloch@sun.com
Mon, 09 Dec 2002 14:39:45 -0800


> In the meantime here are a couple of thoughts:
> (1) ThreadLocal should be generic: 

Agreed.  I think it's a darn shame that Java does not yet have generics. 
 But this is an orthogonal issue.  ThreadLocal will be generic as soon 
as Java supports generics (presumably Tiger).

> (2) There is something really odd about ThreadLocal (an 
> implementation-related notion) showing up in the program's type-space. 

Agreed.  Recall that I originally (in '97) raised the possibility of 
threadlocal as a first-class modifier:

    threadlocal int i;

If we had done that, the actual ThreadLocal object would have been 
hidden.  But without language support, I think this is the best that you 
can do.

> (2') And what exactly does holding a lock on a ThreadLocal accomplish? 
> (I hate the idea of locks on java.lang.Object, but that is another 
> story.) 

I think it's the same story.  It's just an object that you have your 
hands on.  If it uses locks internally for any purpose  (which is 
unspecified) then locking it might jam it up.  Otherwise it's just an 
Object that happens to have a lock.  I'm in agreement with you that 
Java's lock-object isomorphism is questionable, but that's water long 
over the dam.

> if being ThreadLocal is an attribute of a variable, this question 
> would not arise. 

Yep.  But my colleagues were stongly of the opinion that this didn't 
justify a language change.

> (3) Yet another extension to the language via an API, yet another 
> magic class* :-(. 

It's not magic.  It doesn't require VM support.  In fact, it doesn't 
require internal access to any other system class (though performance 
can be and is improved by putting the class "in bed" with Thread).  The 
original implementation of ThreadLocal was written with no support from 
Thread whatsoever.  (InheritableThreadLocal, on the other hand, does 
require support from Thread.)

> If Java were being redesigned from scratch, would ThreadLocal be in 
> there the way it is today? 

I'm not sure.  It could be a modifier, but I'm not sure if it's used 
frequently enough to justify this.  It it weren't a modifier, my guess 
is that we pretty much stick with what we have.



> PS: Twenty years, huh, since we exchanged email? 

   Yep : )