5 Replies Latest reply on Oct 22, 2012 4:33 PM by DrClap

    Non-static ThreadLocal variables

      I want to know what is the actual problem in having non-static ThreadLocal variables ? I am looking for some insights. Java doc says that typically ThreadLocal variables are private and static but does not mention that it has to be mandatorily static and does not provide the reason behind the recommendation for being static. Can anyone please clarify ?
        • 1. Re: Non-static ThreadLocal variables
          Since ThreadLocal variables already guarantee that each thread will have their own copy, adding more instances will only complicate things.

          You can make it non-static, but there is no reason for it and it would imply a very bad and error-prone design.
          • 2. Re: Non-static ThreadLocal variables
            What if I have a code like this i.e. a singleton object having reference to a non-static ThreadLocal variable ?

            public class ThreadLocalTest {
                 private ThreadLocal<Integer> tlocal;
                 private static final ThreadLocalTest tester = new ThreadLocalTest();
                 private ThreadLocalTest() {
                      this.tlocal = new ThreadLocal<Integer>();
                      log("Created ThreadLocalTest instance");
                 public static ThreadLocalTest getInstance() {
                      return tester;
                 public void setValue(int i) {
                      this.tlocal.set(new Integer(i));
                      log("Set value = " + i);
                 public int getValue() {
                      int i = this.tlocal.get();
                      log("Got value = " + i);
                      return i;
                 public static void main(String[] args) {
                      for(int i = 0; i < 2; i++) {               
                           Thread t = new Thread(new Runner());
                           t.setName("Thread-" + i);               
                 public static void log(String msg) {
                      String thread = Thread.currentThread().getName();
                      String date = new java.util.Date().toString();
                      String prefix = "[" + thread + "] " + date + ": ";
                      System.out.println(prefix + msg);
                 static class Runner implements Runnable {          
                      public void run() {
                           for (int i = 0; i < 100; i++) {
                                ThreadLocalTest tester = ThreadLocalTest.getInstance();

            Edited by: radiatejava on Oct 22, 2012 4:56 PM
            • 3. Re: Non-static ThreadLocal variables
              What if I have a code like this i.e. a singleton object having reference to a non-static ThreadLocal variable ?
              What if you do? Why would you do it? You already get one instance per thread. Do you need any more? Do you need it to be thread-local at all? You'e mixing up two different kinds of scoping. Why?
              • 4. Re: Non-static ThreadLocal variables
                My requirement is to have a singleton class/object and the same class having ThreadLocal variables. I need the singleton class for known reasons and at the same time I also need a ThreadLocal variable in that class. How do I achieve that ?
                • 5. Re: Non-static ThreadLocal variables
                  You make the singleton in whatever way you like. You make the ThreadLocal variable static. Why is that a problem?