Wiki Markup |
---|
The {{java.lang.ThreadLocal<T>}} class provides thread-local variables. According to the Java API \[[API 06|AA. Java References#API 06]\]: |
This class These variables differ from their normal counterparts in that each thread that accesses one (via its
get
orset
method) has its own, independently initialized copy of the variable. ThreadLocal instances are typicallyprivate static
fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).
...
This compliant solution uses a custom ThreadPoolExecutor
that extends ThreadPoolExecutor
and overrides the beforeExecute()
method. This method is invoked before the Runnable
task is executed in the specified thread to reinitialize the thread local variable before task r
is executed by thread t
.
...