Get the value in the threadLocal of the parent thread in the java child thread

First, get the value in the threadLocal of the parent thread in the java child thread

threadLocal can do thread-level data isolation, so how to get the value of the parent thread in the child thread? Can use InheritableThreadLocal

/**
 * Java子线程获取父线程的threadLocal中的值
 */
public class ThreadDemo1 {

    public static  final  InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    public static final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) throws Exception {
        inheritableThreadLocal.set("inheritableThreadLocal hello");
        threadLocal.set("threadLocal world");
        new Thread(()->{
            System.out.println(String.format("子线程可继承值:%s", inheritableThreadLocal.get()));
            System.out.println(String.format("子线程值:%s", threadLocal.get()));

            new Thread(()->{
                System.out.println (String.format ("Sun thread can inherit value:% s", inheritableThreadLocal.get ())); 
                System.out.println (String.format ("Sun thread value:% s", threadLocal. get ())); 


            }). start (); 

        }). start (); 


        Thread.sleep (3000); 
        System.out.println ("-------------- can be modified Inherited values ​​of 
        threadLocal and threadLocal -------------------- "); 
        inheritableThreadLocal.set (" inheritableThreadLocal hello2 "); threadLocal.set (" threadLocal world2 "); 


        new Thread (()-> { 
            System.out.println (String.format ("Child thread can inherit value:% s", inheritableThreadLocal.get ())); 
            System.out.println (String.format ("Child thread Value:% s ", threadLocal.get ()));

            new Thread(()->{
                System.out.println (String.format ("Sun thread can inherit value:% s", inheritableThreadLocal.get ())); 
                System.out.println (String.format ("Sun thread value:% s", threadLocal. get ())); 


            }). start (); 

        }). start (); 
    } 
}

  

The output is as follows:

Child thread inheritable value: inheritableThreadLocal hello 
child thread value: null 
grandchild thread inheritable value: inheritableThreadLocal hello 
grandchild thread value: null 
-------------- Modify inheritable values ​​of threadLocal and threadLocal- ------------------- 
Child thread inheritable value: inheritableThreadLocal hello2 
Child thread value: null 
grandchild thread inheritable value: inheritableThreadLocal hello2 
grandchild thread value: null

 Explain that the inheritableThreadLocal value of the parent thread can be obtained in the child thread and grandchild thread. After the value of inheritableThreadLocal is modified, the value of inheritableThreadLocal of the parent thread can also be obtained in the child thread and grandchild thread.

But threadLocal does not work, the value obtained is empty.

 

2. View the source code for creating Thread

 private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        ......
        if (inheritThreadLocals && parent.inheritableThreadLocals != null)
            this.inheritableThreadLocals =
                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
        /* Stash the specified stack size in case the VM cares */
        this.stackSize = stackSize;

        /* Set thread ID */
        tid = nextThreadID();
    }

    //ThreadLocal.createInheritedMap
    static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
        return new ThreadLocalMap(parentMap);
    }

  It can be seen that when the thread is created, the init method is called, and then the inheritableThreadLocals of the parent thread is assigned to inheritableThreadLocals (ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;)

 

Guess you like

Origin www.cnblogs.com/linlf03/p/12687457.html