transmittable-thread-local is a free, open source monitoring & observability project written in Java and released under Apache-2.0. It has 8,306 GitHub stars, 1,722 forks and 57 open issues, and was last pushed 3 months ago. On this registry it ranks #50 of 97 tracked projects in Monitoring & Observability, with 5 head-to-head comparisons available. It gained 5 stars over the last 3 tracked days.

What is transmittable-thread-local?

What it is

TransmittableThreadLocal (TTL) is a small, zero-dependency Java library from Alibaba that provides an enhanced InheritableThreadLocal capable of transmitting values between threads even when those threads are pooled and reused. It lives in the Java ecosystem and targets framework and middleware developers who need reliable context propagation across asynchronous execution boundaries. The library is Apache-2.0 licensed, roughly 1000 SLOC, and supports Java 6 through 21.

The concrete problem it solves is a gap in the JDK. InheritableThreadLocal passes values from a parent thread to a child thread at creation time, but thread pools create threads once and reuse them, so the parent-child relationship no longer holds. What applications actually need is to carry the ThreadLocal value captured at task submission time through to task execution time. TTL extends InheritableThreadLocal and adds a transmitteeValue() hook to control exactly how that value is transferred, closing the gap for any code that runs on ExecutorService, ForkJoinPool, or TimerTask.

Key capabilities

  • Provides TransmittableThreadLocal, which extends InheritableThreadLocal and transmits values across pooled threads.
  • Offers wrappers for ExecutorService, ForkJoinPool, TimerTask, and their thread factories.
  • Supports decorating Runnable and Callable tasks directly.
  • Includes a Java Agent that decorates JDK thread pool implementation classes without code changes.
  • Exposes a protected transmitteeValue() method for customizing how values pass from submission to execution.
  • Ships with integration APIs for frameworks and middleware, alongside user-facing APIs.
  • Runs with zero dependencies and supports Java 6 through 21.

Who uses it and how

  • Distributed tracing systems and full-link stress testing, where a trace or tag identifier must follow a request across thread pool boundaries.
  • Log collection systems that attach request context to log records emitted on worker threads.
  • Request-level caches that need the same request scope visible inside asynchronous tasks.
  • Application containers and upper-layer frameworks that pass information down to lower-layer SDKs running on pooled threads.

Getting started

The library is consumed as a Maven dependency, and the README documents simple usage, thread pool decoration, and Java Agent startup parameters. The stable release line is v2.x on the 2.x branch; this branch carries the in-development v3.

When to use it — and when not to

Use TTL when context must survive thread pool reuse and the alternative is manual propagation at every submission site. Note that transmitted reference objects

project readme (upstream, from github) — read inline

📌 TransmittableThreadLocal(TTL)

[!IMPORTANT] 🚧 这个分支是TransmittableThreadLocal(TTL) v3,在开发中还没有发布。
v3的版本说明、工作项列表及其进展,参见 issue 432

👉 目前使用中的稳定发布版本v2.x分支2.x上。

Fast CI Strong CI Coverage Status JDK support License Javadocs Maven Central GitHub release GitHub Stars GitHub Forks user repos GitHub issues GitHub Contributors GitHub repo size gitpod: Ready to Code

📖 English Documentation | 📖 中文文档



🔧 功能

👉 TransmittableThreadLocal(TTL):在使用线程池等会池化复用线程的执行组件情况下,提供ThreadLocal值的传递功能,解决异步执行时上下文传递的问题。一个Java标准库本应为框架/中间件设施开发提供的标配能力,本库功能聚焦 & 0依赖,支持Java 6~21

JDKInheritableThreadLocal类可以完成父线程到子线程的值传递。但对于使用线程池等会池化复用线程的执行组件的情况,线程由线程池创建好,并且线程是池化起来反复使用的;这时父子线程关系的ThreadLocal值传递已经没有意义,应用需要的实际上是把 任务提交给线程池时ThreadLocal值传递到 任务执行时

本库提供的TransmittableThreadLocal类继承并加强InheritableThreadLocal类,解决上述的问题,使用详见 User Guide

整个TransmittableThreadLocal库的核心功能(用户API、线程池ExecutorService/ForkJoinPool/TimerTask及其线程工厂的Wrapper;开发者API、框架/中间件的集成API),只有 ~1000 SLOC代码行,非常精小。

欢迎 👏

[!NOTE] 从TTL v2.13+开始,升级到Java 8。🚀
如果需要Java 6的支持,使用版本2.12.x Maven Central

🎨 需求场景

ThreadLocal的需求场景即TransmittableThreadLocal的潜在需求场景,如果你的业务需要『在使用线程池等会池化复用线程的执行组件情况下传递ThreadLocal值』则是TransmittableThreadLocal目标场景。

下面是几个典型场景例子。

  1. 分布式跟踪系统 或 全链路压测(即链路打标)
  2. 日志收集记录系统上下文
  3. RequestCache
  4. 应用容器或上层框架跨应用代码给下层SDK传递信息

各个场景的展开说明参见子文档 需求场景

👥 User Guide

使用类TransmittableThreadLocal来保存值,并跨线程池传递。

TransmittableThreadLocal继承InheritableThreadLocal,使用方式也类似。相比InheritableThreadLocal,添加了protectedtransmitteeValue()方法,用于定制 任务提交给线程池时ThreadLocal值传递到 任务执行时 的传递方式,缺省是简单的赋值传递。

注意:如果传递的对象(引用类型)会被修改,且没有做深拷贝(如直接传递引用或是浅拷贝),那么

  • 因为跨线程传递而不再有线程封闭,传递对象在多个线程之间是有共享的。
  • JDKInheritableThreadLocal.childValue()一样,需要使用者/业务逻辑注意保证传递对象的线程安全。
关于transmitteeValue方法 的 展开说明

关于构词后缀eree的说明:

  • transmit是动词传递,transmitter动作的执行者/主动方,而transmittee动作的接收者/被动方。
  • eree后缀的常见词是employer(雇主)/employee(雇员)、caller(调用者)/callee(被调用者)。

具体使用方式见下面的说明。

1. 简单使用

父线程给子线程传递值。

示例代码:

TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();

// =====================================================

// 在父线程中设置
context.set("value-set-in-parent");

// =====================================================

// 在子线程中可以读取,值是"value-set-in-parent"
String value = context.get();

# 完整可运行的Demo代码参见SimpleDemo.kt

这其实是InheritableThreadLocal的功能,应该使用InheritableThreadLocal来完成。

但对于使用线程池等会池化复用线程的执行组件的情况,线程由线程池创建好,并且线程是池化起来反复使用的;这时父子线程关系的ThreadLocal值传递已经没有意义,应用需要的实际上是把 任务提交给线程池时ThreadLocal值传递到 任务执行时

解决方法参见下面的这几种用法。

2. 保证线程池中传递值

2.1 修饰RunnableCallable

使用TtlRunnableTtlCallable来修饰传入线程池的RunnableCallable

示例代码:

T

readme truncated — read the full docs on github

Frequently asked questions

Is transmittable-thread-local free to use?

transmittable-thread-local is open source under the Apache-2.0 licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does transmittable-thread-local do?

📌 a missing Java std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even

What is transmittable-thread-local written in?

transmittable-thread-local is primarily written in Java. Its source is publicly available at https://github.com/alibaba/transmittable-thread-local, and it has 8,306 GitHub stars.