InheritableThreadLocal and Tomcat

Java - 1 Comment » - Posted on May, 8 at 1:18 am

InheritableThreadLocal does not work with tomcat as tomcat thread pool does not clear the thread local contacts after a request has been executed. This results in no good way to share information information between the parent and the child threads that are created using thread local variables.

One way to solve this problem is to maintain a reference to the parent thread by writing your own thread that is constructed by a thread factory.

  1. public class MyThread extends Thread {
  2.         private Thread parent;
  3.         public MyThread(Thread parent) {
  4.                 super();
  5.                 this.parent = parent;
  6.         }
  7.         public Thread getParent() {
  8.                 return parent;
  9.         }
  10. }
  1. import java.util.concurrent.ThreadFactory;
  2. public class MyThreadFactory implements ThreadFactory {
  3.         @Override
  4.         public Thread newThread(Runnable arg0) {
  5.                 return new MyThread(Thread.currentThread());
  6.         }
  7. }
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class ClassNeedingInheritableThreadLocal {
  4.         private Map inheritableThreadLocal=new HashMap();
  5.         public Object getInheritableThreadContext() {
  6.                 return inheritableThreadLocal.get(getThread());
  7.         }
  8.         public void setInheritableThreadContext(Object context) {
  9.                 inheritableThreadLocal.put(getThread(), context);
  10.         }
  11.         private Thread getThread() {
  12.                 Thread currentThread=Thread.currentThread();
  13.                 if(currentThread instanceof MyThread) {
  14.                         currentThread=getParent((MyThread) currentThread);
  15.                 }
  16.                 return currentThread;
  17.         }
  18.         public Thread getParent(MyThread myThread) {
  19.                 Thread parent=myThread.getParent();
  20.                 if(parent instanceof MyThread) {
  21.                         return getParent((MyThread) parent);
  22.                 } else {
  23.                         return parent;
  24.                 }
  25.         }
  26. }

Create a executor service by using our thread factory

  1. ExecutorService service=Executors.newFixedThreadPool(10, new MyThreadFactory());

Remember that you would need to clean up all the objects that were put in the thread local. This can be done with a filter or by using AOP.

Posted in Java | 1 Comment »

One Response to “InheritableThreadLocal and Tomcat”

  1. Hi Rajesh,

    Thank you very much for your excellent article.
    Your article helped me to avoid problems before they appear. :)

    Do you know if this issue persists on recent versions of Tomcat?

    Thanks very much.

    Richard Gomes
    M: 44(77)9955-6813
    http://www.jquantlib.org/index.php/User:RichardGomes
    twitter: frgomes

    Jquantlib is a library for Quantitative Finance written in Java.
    http://www.jquantlib.org/
    twitter: jquantlib

Leave a Reply