Skip to content

嵌入Thread调用的bug #70

Closed
Closed
@aftersss

Description

@aftersss

运行以下代码:

final ExecutorService executorService = Executors.newSingleThreadExecutor();

final InheritableThreadLocal<String> tl1 = new TransmittableThreadLocal<>();

tl1.set("hello");

TtlRunnable runnable = TtlRunnable.get(new Runnable() {
    @Override
    public void run() {
        System.out.println("pool tl1:"+tl1.get());
    }
});
executorService.submit(runnable);
Thread.sleep(200);

new Thread(){
    public void run(){
        TtlRunnable runnable = TtlRunnable.get(new Runnable() {
            @Override
            public void run() {
                System.out.println("pool tl1-2:"+tl1.get());
            }
        },false,false);
        executorService.submit(runnable);
    }
}.start();

输出:

pool tl1:hello
pool tl1-2:null

第二行不应该为null,改成使用InheritableThreadLocal是正常的。

Activity

oldratlee

oldratlee commented on Nov 14, 2016

@oldratlee
Member

@aftersss 收到,我看看

added 2 commits that reference this issue on Nov 14, 2016

add bug #70 reproduce test case, test should fail

add bug #70 reproduce test case, test should fail

oldratlee

oldratlee commented on Nov 14, 2016

@oldratlee
Member

@aftersss 修复在进行中。转成了复现问题的UT,参见提交: bbd4f9e

如果急着解决,先给一个bypass的方法:

new Thread(){
    public void run(){
        // 这里补一行 get
        tl1.get();

        TtlRunnable runnable = TtlRunnable.get(new Runnable() {
            @Override
            public void run() {
                System.out.println("pool tl1-2:"+tl1.get());
            }
        },false,false);
        executorService.submit(runnable);
    }
}.start();
added a commit that references this issue on Nov 14, 2016
oldratlee

oldratlee commented on Nov 14, 2016

@oldratlee
Member

问题已经修复,参见 提交

  1. c5ed61a
  2. 87a02ab

发布了版本 2.1.0这里是release log, Maven依赖:

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

@aftersss 非常赞 👍

嵌套ThreadExecutorServcie这样的使用方式,
之前的UT没有覆盖到,到现在为止的使用中也没有触发这个问题。

非常感谢你的问题反馈!!

modified the milestone: 2.1.0 on Nov 14, 2016
self-assigned this
on Nov 14, 2016
added 2 commits that reference this issue on Nov 14, 2016

add bug #70 reproduce test case, test should fail

aftersss

aftersss commented on Nov 15, 2016

@aftersss
Author

OK,谢谢

oldratlee

oldratlee commented on Nov 15, 2016

@oldratlee
Member

@aftersss 问一下你在什么场景/需求下使用ttl,哪个公司/项目。收集了解一下用户 :)

1 remaining item

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🐞 bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @oldratlee@aftersss

      Issue actions

        嵌入Thread调用的bug · Issue #70 · alibaba/transmittable-thread-local