Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i am setting value of threadlocal in one class can i access the value in another class

Tags:

java

in the below code i am storing temporary threadlocal values in a threadlocal class.and set the value into another class..

List<String> spaces=new ArrayList<String>();
spaces.add(spaceKey);
ThreadLocal<List<String>> threadLocal=new ThreadLocal<List<String>>();
threadLocal.set(spaces);  
setShardHolder(threadLocal);

i have getters and setters in another class

private ThreadLocal<List<String>> shardHolder;
public ThreadLocal<List<String>> getShardHolder() {
return shardHolder;
}
public void setShardHolder(ThreadLocal<List<String>> shardHolder) {
this.shardHolder = shardHolder;
}

i am not able to access the values using the getter

like image 508
Vijay Rahul Avatar asked Jul 12 '13 12:07

Vijay Rahul


People also ask

What can the ThreadLocal class be used for?

The ThreadLocal class is used to create thread local variables which can only be read and written by the same thread. For example, if two threads are accessing code having reference to same threadLocal variable then each thread will not see any modification to threadLocal variable done by other thread.

When should ThreadLocal be removed?

You should always call remove because ThreadLocal class puts values from the Thread Class defined by ThreadLocal. Values localValues; This will also cause to hold reference of Thread and associated objects. the value will be set to null and the underlying entry will still be present.

Can ThreadLocal cause memory leak?

Memory leak is caused when ThreadLocal is always existing. If ThreadLocal object could be GC, it will not cause memory leak. Because the entry in ThreadLocalMap extends WeakReference, the entry will be GC after ThreadLocal object is GC.

Should Java ThreadLocal be static?

ThreadLocal s should be stored in static variables to avoid memory leaks. If a ThreadLocal is stored in an instance (non-static) variable, there will be M \* N instances of the ThreadLocal value where M is the number of threads, and N is the number of instances of the containing class.


1 Answers

You don't seem to have understood how ThreadLocal variables work. A ThreadLocal instance is a bit like a container for a single value. If you store something in it, it will be associated to the current thread, and you can retrieve it later, from the same thread.

But of course, if you store something in a ThreadLocal instance, and try to retrieve it from a different ThreadLocal instance, you'll always get null: they're not the same container.

So a ThreadLocal is often hidden behind static methods. Suppose you want to associate a user with the current thread, for example. You would do

public class CurrentUser {
    private static final ThreadLocal<User> CURRENT = new ThreadLocal<User>();

    public static User getCurrentUser() {
        return CURRENT.get();
    }

    public static void setCurrentUser(User user) {
        CURRENT.set(user);
    }
}

Then let's design a scenario :

public class Test
    public static void main(String[] args) {
        CurrentUser.set(new User("foo"));
        System.out.println(CurrentUser.get()); // prints foo
        new Thread(new Runnable() {
            public void run() {
                System.out.println(CurrentUser.get()); // prints null: different thread
            }
        }).start();
    }
}
like image 102
JB Nizet Avatar answered Sep 20 '22 03:09

JB Nizet