Possible Duplicate:
How is Java’s ThreadLocal implemented under the hood?
It's been a while since I programmed in Java, and I want to freshen up on some theory.
How can I implement my own ThreadLocal from scratch?
Possible Duplicate:
How is Java’s ThreadLocal implemented under the hood?
It's been a while since I programmed in Java, and I want to freshen up on some theory.
How can I implement my own ThreadLocal from scratch?
Yes. You can sub-class thread and copy the implementation in the source for Thread. Or you can just read the code but getting ThreadLocal right is tricky.
There are many tricky elements, the most obvious is preventing memory leaks from threads not being removed.
Even the existing implementation suffers from the issue that sub-classed ThreadLocal objects can prevent ClassLoaders from unloading.
try this out:
new Thread()
{
//contents
}.start();
You can spawn this anywhere you want in your code.
It turns out this question is a duplicate:
It's quite simple, really, it's just a Map of name-value pairs stored inside each Thread object (see the Thread.threadLocals field). The API hides that implementation detail, but that's more or less all there is to it.