This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details

The opening

This is the 9th day of my participation in Gwen Challenge

Data is read from the thread where it is stored, but not from the child thread. So what if I want to read it?

The sample

We use InheritableThreadLocal, a subclass of ThreadLocal, to help solve these problems. We’ll use InheritableThreadLocal and ThreadLocal to demonstrate the effects for a little bit more intuitive understanding.

@Test void contextLoads() throws InterruptedException { ThreadLocal threadLocal = new ThreadLocal(); Threadlocal.set (" threadlocal.set "); System.out.println("threadLocal.get() = " + threadLocal.get()); New Thread(() -> {String name = thread.currentThread ().getName(); Format (" This is sub thread name: %s, threadLocale.get (): %s", name, threadLocale.get ()))); }).start(); Thread.sleep(1000); InheritableThreadLocal = new InheritableThreadLocal(); InheritableThreadLocal. Set (" old wet inheritableThreadLocal jiang "); System.out.println("inheritableThreadLocal.get() = " + inheritableThreadLocal.get()); // The child thread can also fetch data from the parent thread ThreadLocal. new Thread(() -> { String name = Thread.currentThread().getName(); System. Out.println (String format (" this is sub thread name: % s, inheritableThreadLocal. The get () : %s", name, inheritableThreadLocal.get())); }).start(); }Copy the code

InheritableThreadLocal interpretation

The above three methods are as follows:

  • GetMap () : The return value of the method becomes an inheritableThreadLocals object
  • CreateMap (): Builds objects from ThreadLocalMap and copies them to inheritableThreadLocals
  • ChildValue (): This method only returns its input parameter and should be overridden

Compared to ThreadLocal, the main object that holds the data changes from the threadLocals attribute to the inheritableThreadLocals attribute.

The set process

ThreadLocal#set(T value)

Will calljava.lang.InheritableThreadLocal#createMapThat is, giveThreadOf the classinheritableThreadLocalsVariable assignment.

When is createMap called

If the parent thread exists in the inheritableThreadLocals variable and is not null, Call the ThreadLocal. CreateInheritedMap () for this thread inheritableThreadLocals variable assignment.

ThreadLocal createInheritedMap method does, in fact is the parent thread inheritableThreadLocals variable values assigned to the child thread inheritableThreadLocals variables. Therefore, the child thread can access the data in the parent thread ThreadLocal.

This replication is not real-time synchronization. It assigns the inheritableThreadLocals variable of the parent thread to the child thread as soon as it is created. Once the child thread is created, When the user changes the value of the parent thread’s inheritableThreadLocals variable, the child thread is unaware of the change. So this is value passing, not reference passing.

Ok, so what happens with InheritableThreadLocal

👍 ❤️ Don’t get lost

The article continues to update every week, you can search wechat “ten minutes to learn programming” the first time to read and urge more, if this article is not bad, feel something if you support and recognition, is the biggest power of my creation, we will see the next article!