1 Star 0 Fork 2

wudongqiang / multi-thread-context

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README-EN.md 10.26 KB
一键复制 编辑 原始数据 按行查看 历史
oldratlee 提交于 2017-06-25 20:19 . update README-EN.md

Transmittable ThreadLocal(TTL)

Build Status Coverage Status Maven Central GitHub release Dependency Status
Join the chat at https://gitter.im/alibaba/transmittable-thread-local GitHub issues Percentage of issues still open Average time to resolve an issue License

:book: English Documentation | :book: 中文文档



:wrench: Functions

:point_right: 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.

:art: Requirements

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.

:busts_in_silhouette: User Guide

1. 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.

2. Transmit value even using thread pool

2.1 Decorate 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(); 

2.2 Decorate thread pool

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(); 

2.3. Use Java Agent to decorate thread pool implementation class

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 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".

:electric_plug: Java API Docs

The current version Java API documentation: http://alibaba.github.io/transmittable-thread-local/apidocs/

:cookie: Maven dependency

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>transmittable-thread-local</artifactId>
	<version>2.1.0</version>
</dependency>

Check available version at search.maven.org.

:books: Related resources

Jdk core classes

Java Agent

Javassist

1
https://gitee.com/wudongqiang/multi-thread-context.git
git@gitee.com:wudongqiang/multi-thread-context.git
wudongqiang
multi-thread-context
multi-thread-context
master

搜索帮助