What is the typical use of ThreadLocal in java. When does one use it? I couldn't application details from this java docs article.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With