Saturday 15 March 2014

Why is java.lang.ThreadLocal a map on Thread instead on the ThreadLocal? -


Knightley, I expected that a threadline would be to type the value of some kind of weaker hashp thrah. So I had a little trouble when I came to know that the value of threadlocals is actually why it was done? I hope that will not be connected to the threadlocal if the price will be saved in the pulse.

Explanation: I was thinking like something

  public class alternate, and Lieutenant T & gt; {Private Final Map & lt; Thread, T & gt; Value = collection. Synchronized maps (new WeakHashMap & lt; thread, T & gt; ()); Public zeros set (t value) {values.put (Thread.currentThread (), value); } Receive public t () {return values.get (thread.startThread ());}}  

As far as I can see it will stop the strange problem, not the threadlock and Nor can be left at prices until the trash is accumulated till the death of the thread, if the value somehow references to the ThreadLocal (perhaps the most devious form of this is when the threadlocal is a value on a class There is a fixed variable in context. Now Rake has a large resource leak at the rearrangement in the application server, as neither the objects nor their classes can be collected.)

Sometimes you get enlightened by only asking a question. :-) Now I have seen a possible answer: Thread-security If the value of the map is in the thread object, then entering a new value is a minor-if the map is on the threadline, then you have issues of general concurrency, which slow down things can do. (Of course you will use the Readwralock instead of synchronizing, but the problem remains.)


No comments:

Post a Comment