當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.ThreadLocal用法及代碼示例

該類提供線程局部變量。這些變量與其正常對應變量的不同之處在於,訪問一個變量(通過其 get 或 set 方法)的每個線程都有其自己的、獨立初始化的變量副本。本質上,這是除了編寫不可變類之外實現線程安全的另一種方法。由於對象不再共享,因此不需要同步,這可以提高應用程序的可擴展性和性能。 ThreadLocal提供線程限製,它是局部變量的擴展。 ThreadLocal僅在單個線程中可見。沒有兩個線程可以看到彼此的線程局部變量。這些變量通常是類中的私有靜態字段,並在線程內維護它們的狀態。

Note: ThreadLocal class extends Object class

構造函數:ThreadLocal():這將創建一個線程局部變量。

ThreadLocal類的方法

方法 執行的操作
get() 返回此線程局部變量的當前線程副本中的值。如果該變量對於當前線程沒有值,則首先將其初始化為調用 initialValue() 方法返回的值
initialValue() 返回本地線程變量的當前線程初始值。
remove() 刪除此線程局部變量的當前線程值。如果當前線程隨後讀取此線程局部變量,則將通過調用其 initialValue() 方法重新初始化其值,除非當前線程在此期間設置其值。這可能會導致當前線程中多次調用initialValue方法
set() 將此線程局部變量的當前線程副本設置為指定值。大多數子類不需要重寫此方法,僅依靠initialValue()方法來設置線程局部變量的值。

示例 1:

Java


// Java Program to Illustrate ThreadLocal Class
// Via get() and set() Method
// Class
// ThreadLocalDemo
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating objects of ThreadLocal class
        ThreadLocal<Number> gfg_local
            = new ThreadLocal<Number>();
        ThreadLocal<String> gfg = new ThreadLocal<String>();
        // Now setting custom value
        gfg_local.set(100);
        // Returns the current thread's value
        System.out.println("value = " + gfg_local.get());
        // Setting the value
        gfg_local.set(90);
        // Returns the current thread's value of
        System.out.println("value = " + gfg_local.get());
        // Setting the value
        gfg_local.set(88.45);
        // Returns the current thread's value of
        System.out.println("value = " + gfg_local.get());
        // Setting the value
        gfg.set("GeeksforGeeks");
        // Returning the current thread's value of
        System.out.println("value = " + gfg.get());
    }
}
輸出
value = 100
value = 90
value = 88.45
value = GeeksforGeeks

示例 2:

Java


// Java Program to Illustrate ThreadLocal Class
// Via Illustrating remove() Method
// Class
// ThreadLocalDemo
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating objects of ThreadLocal class
        ThreadLocal<Number> gfg_local
            = new ThreadLocal<Number>();
        ThreadLocal<String> gfg = new ThreadLocal<String>();
        // Setting the value
        gfg_local.set(100);
        // Returning the current thread's value
        System.out.println("value = " + gfg_local.get());
        // Setting the value
        gfg_local.set(90);
        // Returns the current thread's value of
        System.out.println("value = " + gfg_local.get());
        // Setting the value
        gfg_local.set(88.45);
        // Returning the current thread's value of
        System.out.println("value = " + gfg_local.get());
        // Setting the value
        gfg.set("GeeksforGeeks");
        // Returning the current thread's value of
        System.out.println("value = " + gfg.get());
        // Removing value using remove() method
        gfg.remove();
        // Returning the current thread's value of
        System.out.println("value = " + gfg.get());
        // Removing value
        gfg_local.remove();
        // Returns the current thread's value of
        System.out.println("value = " + gfg_local.get());
    }
}
輸出
value = 100
value = 90
value = 88.45
value = GeeksforGeeks
value = null
value = null

示例 3:

Java


// Java Program to Illustrate ThreadLocal Class
// Via initialValue() Method
// Importing required classes
import java.lang.*;
// Class 1
// Helper class extending Thread class
class NewThread extends Thread {
    private static ThreadLocal gfg = new ThreadLocal() {
        protected Object initialValue()
        {
            return new Integer(question--);
        }
    };
    private static int question = 15;
    NewThread(String name)
    {
        // super keyword refers to parent class instance
        super(name);
        start();
    }
    // Method
    // run() method for Thread
    public void run()
    {
        for (int i = 0; i < 2; i++)
            System.out.println(getName() + " " + gfg.get());
    }
}
// Class 2
// Main class
// ThreadLocalDemo
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating threads inside main() method
        NewThread t1 = new NewThread("quiz1");
        NewThread t2 = new NewThread("quiz2");
    }
}

輸出:



相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.lang.ThreadLocal Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。