-6

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 .

Community
  • 1
  • 1
kannanrbk
  • 6,964
  • 13
  • 53
  • 94

3 Answers3

2

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

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
1

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
0

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.

Sumit Desai
  • 1,542
  • 9
  • 22