Propagating ThreadLocal to a new Thread fetched from a ExecutorService

后端 未结 6 758
傲寒
傲寒 2020-12-02 11:31

I\'m running a process in a separate thread with a timeout, using an ExecutorService and a Future (example code here) (the thread \"spawning\" takes place in a AOP Aspect).<

6条回答
  •  盖世英雄少女心
    2020-12-02 12:13

    If you look at ThreadLocal code you can see:

        public T get() {
            Thread t = Thread.currentThread();
            ...
        }
    

    current thread cannot be overwritten.

    Possible solutions:

    1. Look at java 7 fork/join mechanism (but i think it's a bad way)

    2. Look at endorsed mechanism to overwrite ThreadLocal class in your JVM.

    3. Try to rewrite RESTEasy (you can use Refactor tools in your IDE to replace all ThreadLocal usage, it's look like easy)

提交回复
热议问题