Why is ThreadLocal not using java.util.HashMap to store the value, but configuring it?

I am reading the java.lang.ThreadLocal source code, I have two questions:

  • Why doesn't ThreadLocal use java.util.HashMap to store the value, but customize it?
  • Why is ThreadLocalMap defined statically in ThreadLocal? if not static how can it be?
+3


source to share


1 answer


Looking at the earliest JDK 1.2 source code, the dates in the code show that ThreadLocal came before HashMap.

  * <this is HashMap>
  * @author  Josh Bloch
  * @author  Arthur van Hoff
  * @version 1.29, 04/22/99
  * @see     Object#hashCode()
  * @see     Collection
  * @see        Map
  * @see        TreeMap
  * @see        Hashtable
  * @since JDK1.2

  * <this is ThreadLocal>
  * @author  Josh Bloch
  * @version 1.8 07/08/98
  * @since   JDK1.2
  */

      

Ever since the same author, I suspect that he wrote a specific one and later replaced the general one. I don't know if there was another implementation of HashMap prior to this version, but if it looked like Josh Bloch didn't want to use it.



Also, it looks like the implementation is ThreadLocal

optimized with a custom function like getEntryAfterMiss

expungeStaleEntry

etc. So while another map implementation is fine for the general case, you have this highly custom map implementation already working.

Regarding your second question, http://www.geeksforgeeks.org/static-class-in-java/ .

+1


source







All Articles