Home » Java » java – Accessing ThreadLocal values from a method which a thread has invoked?-Exceptionshub

java – Accessing ThreadLocal values from a method which a thread has invoked?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have used ThreadLocal very little, however I need to know about it for work reasons. I did search on this, and came to know about the concept.

I made sample example below:

ThreadLocalMain.java -> Main driver program

package com.example.threads.threadlocal;

public class ThreadLocalMain {
    public static void main(String[] args) throws InterruptedException {
        Thread1 t1 = new Thread1("First");
        Thread1 t2 = new Thread1("Second");
        Thread1 t3 = new Thread1("Third");

        t1.start();
        t2.start();
        t3.start();
    }
}

Thread1.java -> Which uses ThreadLocal

package com.example.threads.threadlocal;

import com.example.threads.threadlocal.utils.Util1;

public class Thread1 extends Thread {

    private static Integer unique_id = 0;
    @SuppressWarnings("rawtypes")
    private static ThreadLocal tl = new ThreadLocal() {

        @Override
        public Integer initialValue() {
            return (++unique_id);
        }
    };

    public Thread1(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("Current thread is --> " + Thread.currentThread().getName() + " with thread local value as --> " + tl.get());

        Util1 u1 = new Util1();
        u1.display(tl);
    }

Util.java –> In this I want to check Thread’s ThreadLocal value


package com.example.threads.threadlocal.utils;

public class Util1 {
    public void display(ThreadLocal tl) {
        System.out.println("Inside Util1#display() method, executed by " + Thread.currentThread().getName()
                + " my thread local value is -->  " + tl.get());
    }
}

I have main driver program, which creates 3-thread, and in thread class there is ThreadLocal which saves values for current thread.

My question is how can we print the value of ThreadLocal for current thread which has invoked a method on some object. In this case the object is Util. I was able to do by passing the instance of ThreadLocal into Util's display() method.

So my question:

Is it possible to print the value of ThreadLocal in a method without passing ThreadLocal reference to the method? Is it feasible at all? Is my method not the way it is intended to use ThreadLocal.

How to&Answers:

As far as you do not execute code in different threads in display method, you can pass value of thread local instead the thread local to display method. Following code will lead the same result

@Override
public void run() {
      System.out.println("Current thread is --> " + Thread.currentThread().getName() + " with thread local value as --> " + tl.get());

    Util u1 = new Util();
    u1.display(tl.get());
}

public class Util {
  public void display(Object value) {
    System.out.println("Inside Util1#display() method, executed by " + Thread.currentThread().getName()
            + " my thread local value is -->  " + value);
}

}