👉 Transmit ThreadLocal
value between threads, even using thread cached components like thread pool.
Class InheritableThreadLocal
in JDK
can transmit value to child thread from parent thread.
But when use thread pool, thread is cached up and used repeatedly. Transmitting value from parent thread to child thread has no meaning. Application need transmit value from the time task is created to the time task is executed.
If you have problem or question, please submit Issue or play fork and pull request dance.
The Requirements listed below is also why I sort out TTL
in my work.
- Application container or high layer framework transmit information to low layer sdk.
- Transmit context to logging without application code aware.
- simple usage
// set in parent thread
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
// =====================================================
// read in child thread, value is "value-set-in-parent"
String value = parent.get();
This is the function of class InheritableThreadLocal
, should use class InheritableThreadLocal
instead.
But when use thread pool, thread is cached up and used repeatedly. Transmitting value from parent thread to child thread has no meaning. Application need transmit value from the time task is created to the point task is executed.
The solution is below usage.
- Transmit value even using thread pool
Decorate input Runnable
and Callable
by com.alibaba.ttl.TtlRunnable
and com.alibaba.ttl.TtlCallable
.
Sample code:
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
Runnable task = new Task("1");
// extra work, create decorated ttlRunnable object
Runnable ttlRunnable = TtlRunnable.get(task);
executorService.submit(ttlRunnable);
// =====================================================
// read in task, value is "value-set-in-parent"
String value = parent.get();
above code show how to dealing with Runnable
, Callable
is similar:
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
Callable call = new Call("1");
// extra work, create decorated ttlCallable object
Callable ttlCallable = TtlCallable.get(call);
executorService.submit(ttlCallable);
// =====================================================
// read in call, value is "value-set-in-parent"
String value = parent.get();
Eliminating the work of Runnable
and Callable
Decoration every time it is submitted to thread pool. This work can completed in the thread pool.
Use util class
com.alibaba.ttl.threadpool.TtlExecutors
to decorate thread pool.
Util class com.alibaba.ttl.threadpool.TtlExecutors
has below methods:
getTtlExecutor
: decorate interfaceExecutor
getTtlExecutorService
: decorate interfaceExecutorService
ScheduledExecutorService
: decorate interfaceScheduledExecutorService
Sample code:
ExecutorService executorService = ...
// extra work, create decorated executorService object
executorService = TtlExecutors.getTtlExecutorService(executorService);
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
Runnable task = new Task("1");
Callable call = new Call("2");
executorService.submit(task);
executorService.submit(call);
// =====================================================
// read in Task or Callable, value is "value-set-in-parent"
String value = parent.get();
In this usage, transmission is transparent(no decoration operation).
Sample code:
ExecutorService executorService = Executors.newFixedThreadPool(3);
Runnable task = new Task("1");
Callable call = new Call("2");
executorService.submit(task);
executorService.submit(call);
// =====================================================
// Task或是Call中可以读取, 值是"value-set-in-parent"
String value = parent.get();
See demo AgentDemo.java
.
Agent decorate 2 thread pool implementation classes
(implementation code TtlTransformer.java
):
java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ScheduledThreadPoolExecutor
Add start options on Java command:
-Xbootclasspath/a:/path/to/transmittable-thread-local-2.x.x.jar
-javaagent:/path/to/transmittable-thread-local-2.x.x.jar
NOTE:
- Agent modify the jdk classes, add code refer to the class of
TTL
, so the jar ofTTL Agent
should add tobootclasspath
. TTL Agent
modify the class byjavassist
, so the Jar ofjavassist
should add tobootclasspath
too.
Java command example:
java -Xbootclasspath/a:transmittable-thread-local-2.0.0.jar \
-javaagent:transmittable-thread-local-2.0.0.jar \
-cp classes \
com.alibaba.ttl.threadpool.agent.demo.AgentDemo
Run the script run-agent-demo.sh
to start demo of "Use Java Agent to decorate thread pool implementation class".
The current version Java API documentation: http://alibaba.github.io/transmittable-thread-local/apidocs/
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.0.0</version>
</dependency>
Check available version at search.maven.org.