10

For example, we have a static ThreadLocal field an a setter:

private static final ThreadLocal threadLocalField = new ThreadLocal;

public static void getSXTransaction() {
  threadLocalField.set(new MyValue());
}

I wonder, what are the guarantees of thread safety here as there is no implicit synchronization in the java.lang.ThreadLocal#set method? I know it makes sense for the TreadLocal class to be fully thread safe by it's nature but I cannot understand how it's being accomplished.

Here is the source code for it:

/**
 * Sets the current thread's copy of this thread-local variable
 * to the specified value.  Most subclasses will have no need to 
 * override this method, relying solely on the {@link #initialValue}
 * method to set the values of thread-locals.
 *
 * @param value the value to be stored in the current thread's copy of
 *        this thread-local.
 */
public void set(T value) {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
}

2 Answers 2

21

It's safe because getMap returns the map for the given (i.e. current) thread. No other thread is going to be messing with that. So it's really down to the implementation of getMap to make sure that's is okay for any thread - and as far as I can see, that just delegates to a field within the Thread object. It's not clear to me whether getMap is ever passing in any thread other than the current thread - if it were, that could be potentially tricky - but I suspect it's all been written carefully to make sure that's not a problem :)

7
  • 2
    I believe that using ThreadLocal is bad practice and looks like a hack.
    – DontRelaX
    Commented Nov 28, 2014 at 12:11
  • 2
    @DontRelaX: I don't see how this is relevant to the question or the answer. I somewhat agree, although having some notion of context can sometimes be helpful - but I don't see how the comment is helpful on this question or answer.
    – Jon Skeet
    Commented Nov 28, 2014 at 12:15
  • @Jon So it's really down to the implementation of getMap to make sure that's is okay for any thread - thanks for clearing this to me!
    – iozee
    Commented Nov 28, 2014 at 12:15
  • It's possible for another thread to get the map by reflecting the Thread object's threadlocals. This should be another path to set this map.
    – Chao
    Commented Sep 5, 2016 at 15:46
  • 2
    @Richard: Right, and my point is that it would be pointless to add "None of this answer applies if you use reflection to violate normal behaviour" to almost every Java answer on Stack Overflow.
    – Jon Skeet
    Commented Sep 5, 2016 at 15:52
0

Adding on to Jon's answer, I agree, the map is the sole property of the current thread.

ThreadLocalMap getMap(Thread t) {
    return t.threadLocals;
}

And this ThreadLocalMap stores a table inside which only refers to the calling ThreadLocal object (which sits in the Heap space, i.e. common to all threads) for getting hash index into the table. But the actual value is stored inside this table which is per thread.

So if your ThreadLocal object is shared between multiple threads and each thread call get or set at the same time on it, they're literally setting the values in their own copies of the table contained inside ThreadLocalMap which is stored inside Thread class as a non static field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.