Possible Duplicate:
When and how should I use a ThreadLocal variable?
What I know about thread local is .
1) Thread Local must be declared as public static final .
2) Thread Local is a Local Variable for current thread .
Possible Duplicate:
When and how should I use a ThreadLocal variable?
What I know about thread local is .
1) Thread Local must be declared as public static final .
2) Thread Local is a Local Variable for current thread .
it doesn't need to be public
but it is essentially a Map where you can only get for the current Thread as key and when the value hasn't been set yet it is initialized automatically
this implements thread local storage so you can have "public static" variables without needing to synchronize on them
Can't find question in your statements, but let me try:
1) Thread Local must be declared as public static final .
Not really, in fact ThreadLocal
can be private, doesn't have to be static
and can be non-final. Don't know where have you seen such requirements.
2) Thread Local is a Local Variable for current thread .
Correct. You can imagine that ThreadLocal
is a map where key is a thread and value is the actual variable. But it is implemented in much better way.
A Threadlocal creates separate instance of the type you specified(It is generic) per thread. You can access it simply using it's get() method. This method will always return value for current thread.