[concurrency-interest] Bug in ThreadLocal ?

Jason Mehrens jason_mehrens at hotmail.com
Fri Apr 13 15:29:45 EDT 2007


ThreadLocal invokes "initialValue" not "initValue".  Are you using 
"@Override"?

Jason Mehrens

public static void main(String[] args) {
    ThreadLocal<Boolean> t = new ThreadLocal<Boolean>() {
      @Override protected Boolean initialValue() {
        System.out.println("initialValue");
        return new Boolean(true);
      }
    };

    Boolean first = t.get();
    t.remove();
    Boolean second = t.get();
    System.out.println("Same object? "+ (first == second));
  }

==========================================
initialValue
initialValue
Same object? false




>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

_________________________________________________________________
MSN is giving away a trip to Vegas to see Elton John.  Enter to win today. 
http://msnconcertcontest.com?icid-nceltontagline



More information about the Concurrency-interest mailing list