同步操作将从 Gitee 极速下载/multi-thread-context 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
English Documentation | 中文文档
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.
// 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.
Runnable
and Callable
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 interface Executor
getTtlExecutorService
: decorate interface ExecutorService
ScheduledExecutorService
: decorate interface ScheduledExecutorService
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:
TTL
, so the jar of TTL Agent
should add to bootclasspath
.TTL Agent
modify the class by javassist
, so the Jar of javassist
should add to bootclasspath
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.1.0</version>
</dependency>
Check available version at search.maven.org.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。