Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uses of ThreadLocal

Tags:

java

What is the typical use of ThreadLocal in java. When does one use it? I couldn't application details from this java docs article.

like image 298
Noriyasu Avatar asked Oct 18 '10 07:10

Noriyasu


Video Answer


1 Answers

I'd say that the most typical ThreadLocal usage is when you have some object that has to be accessible everywhere during one flow and you do not want to pass reference to this object over all layers. Something like singleton pattern but per thread.

The examples are DB connection, hibernate session etc. You open them somewhere in the beginning of your flow, commit/close in the end of the flow and use everywhere during the flow.

like image 123
AlexR Avatar answered Sep 23 '22 13:09

AlexR