[concurrency-interest] Bug in ThreadLocal ?

Thomas Hawtin tackline at tackline.plus.com
Fri Apr 13 15:48:50 EDT 2007

Hanson Char wrote:
> So I would expect, within the same thread, a ThreadLocal#get followed by 
> ThreadLocal#remove followed by ThreadLocal#get would result in 
> ThreadLocal#initialValue being invoked twice.
> But this is not the case.  The last "get" did not cause the "initValue" 
> to be invoked.  When I looked at the code of the "remove" and "get" 
> method implementation, it seems there is a bug in that the internal " 
> t.threadLocals" should be set to null when the remove method is invoked, 
> but is not currently the case.

Works for me...

class RemoveTest {
     public static void main(String[] args) {
         ThreadLocal<Void> local = new ThreadLocal<Void>() {
             protected Void initialValue() {
                 return null;

Have you got some code to demonstrate the problem?

Tom Hawtin

More information about the Concurrency-interest mailing list