📜  带有示例的Java.lang.InheritableThreadLocal 类

📅  最后修改于: 2022-05-13 01:54:48.232000             🧑  作者: Mango

带有示例的Java.lang.InheritableThreadLocal 类

Java.lang.InheritableThreadLocal 类扩展了 ThreadLocal 以提供从父线程到子线程的值继承:当创建子线程时,子线程接收父线程具有值的所有可继承线程局部变量的初始值。

父线程, ThreadLocal变量默认对子线程不可用。
构造函数

InheritableThreadLocal gfg_tl = new InheritableThreadLocal();

它是ThreadLocal的子类,因此默认情况下ThreadLocal中存在的所有方法都可用于InheritableThreadLocal
它只包含一种方法:
句法 :

public Object childValue(Object parentValue) 

在子线程启动之前,该方法在父线程中被调用(覆盖)。

  • 如果我们想让父线程,线程局部变量值对子线程可用,那么我们应该去InheritableThreadLocal类。
  • 默认情况下,子线程值与父线程值完全相同。但是我们可以通过覆盖childValue方法为子线程提供我们自己的自定义值。

例子:

// Java program to illustrate parent thread, ThreadLocal variable
// by default not available to child thread
  
class ParentThread extends Thread {
    public static ThreadLocal gfg_tl = new ThreadLocal();
    public void run()
    {
  
        // setting the new value
        gfg_tl.set("parent data");
  
        // returns the ThreadLocal value associated with current thread
        System.out.println("Parent  Thread Value :" + gfg_tl.get());
  
        ChildThread gfg_ct = new ChildThread();
        gfg_ct.start();
    }
}
  
class ChildThread extends Thread {
    public void run()
    {
  
        // returns  the ThreadLocal value associated with current thread
        System.out.println("Child Thread Value :" + ParentThread.gfg_tl.get());
        /* null (parent thread variable 
        thread local value is not available to child thread ) */
    }
}
class ThreadLocalDemo {
    public static void main(String[] args)
    {
        ParentThread gfg_pt = new ParentThread();
        gfg_pt.start();
    }
}
Output:
Parent Thread Value:parent data
Child Thread Value:null (by default initialValue is null)
// Java program to illustrate inheritance of customized value
// from parent thread to child thread
  
class ParentThread extends Thread {
    // anonymous inner class  for overriding childValue method.
    public static InheritableThreadLocal gfg_tl = new InheritableThreadLocal() {
        public Object childValue(Object parentValue)
        {
            return "child data";
        }
    };
    public void run()
    {
        // setting the new value
        gfg_tl.set("parent data");
        // parent data
        System.out.println("Parent Thread Value :" + gfg_tl.get());
  
        ChildThread gfg_ct = new ChildThread();
        gfg_ct.start();
    }
}
class ChildThread extends Thread {
  
    public void run()
    {
        // child data
        System.out.println("Child Thread Value :" + ParentThread.gfg_tl.get());
    }
}
class ThreadLocalDemo {
  
    public static void main(String[] args)
    {
        ParentThread gfg_pt = new ParentThread();
        gfg_pt.start();
    }
}
Output:
Parent Thread Value:parent data 
Child Thread Value:child data

第一种情况:在上面的程序中,如果我们用 ThreadLocal 替换 InheritableThreadLocal 并且我们没有覆盖 childValue 方法,那么输出是:

Output:
Parent Thread Value: parent data 
Child Thread Value:null   (by default initialValue is null)

第二种情况:在上面的程序中,如果我们维护 InheritableThreadLocal 并且我们没有覆盖 childValue 方法,那么输出是:

Output :
Parent Thread Value:parent data 
Child Thread Value:parent data

第三种情况:在上面的程序中,如果我们维护 InheritableThreadLocal 并且我们还覆盖了 childValue 方法,那么输出是:

Output:
Parent Thread Value:parent data 
Child Thread Value:child data