When multiple threads access a ThreadLocal instance, separate copy of Threadlocal variable is maintained for each thread.
USAGE
Example 1. Creating DAO classes
Database connection can be maintained separately for each thread.
Each thread can have its own connection.
It can maintain a global connection that is initialized at startup (per thread) to avoid passing a connection to every method.
Example 2. Maintaining current login user
We can maintain user data for each login request.
Example 3. Implementation of JEE containers
JEE containers serves multiple requests in the web servers.
Example 4. Maintaining transaction context
A static ThreadLocal can hold the transaction context. This can be used by JEE containers in order to check the current running transaction.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.