Java中的Java.lang.ThreadLocal类

此类提供线程局部变量。这些变量不同于普通的对应变量,因为每访问一个(通过其get或set方法)线程都有自己独立初始化的变量副本。

  • 基本上,除了编写不可变的类之外,它是实现线程安全的另一种方法。
  • 由于对象不再是共享的,所以不需要同步来提高应用程序的可伸缩性和性能。
  • 它扩展了类对象。
  • ThreadLocal提供线程限制,它是局部变量的扩展。ThreadLocal只在单线程中可见。没有两个线程可以看到彼此的线程局部变量。
  • 这些变量通常是类中的私有静态字段,并在线程中维护其状态。

构造方法:
ThreadLocal():这将创建一个线程局部变量。
方法:

  1. Object get():此方法返回此线程局部变量的当前线程副本中的值。如果变量对于当前线程没有值,则首先将其初始化为调用initialValue()方法返回的值。

  2. void set(Object value):此方法将此线程局部变量的当前线程副本设置为指定值。大多数子类不需要重写这个方法,只依赖initialValue()方法来设置线程局部变量的值。

    复制代码
    // Java code illustrating get() and set() method 
    
    public class ThreadLocalDemo { 
    
    public static void main(String[] args) 
        { 
    
            ThreadLocal<Number> gfg_local = new ThreadLocal<Number>(); 
    
            ThreadLocal<String> gfg = new ThreadLocal<String>(); 
            // setting the 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"); 
    
            // returns the current thread's value of 
            System.out.println("value = " + gfg.get()); 
        } 
    } 
    复制代码

    输出:

    value = 100
    value = 90
    value = 88.45
    value = GeeksforGeeks

     

  3. void remove():此方法删除此线程局部变量的当前线程值。如果它的当前线程的值随后被它的当前线程初始化,除非它的当前线程的值被初始化。这可能导致在当前线程中多次调用initialValue方法。

    复制代码
    // Java code illustrating remove() method 
    
    public class ThreadLocalDemo { 
    
    public static void main(String[] args) 
        { 
    
            ThreadLocal<Number> gfg_local = new ThreadLocal<Number>(); 
    
            ThreadLocal<String> gfg = new ThreadLocal<String>(); 
    
            // setting the 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"); 
    
            // returns the current thread's value of 
            System.out.println("value = " + gfg.get()); 
    
            // removing value 
            gfg.remove(); 
    
            // returns the current thread's value of 
            System.out.println("value = " + gfg.get()); 
    
            // removing vale 
            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
  4. initialValue():此方法返回此线程局部变量的当前线程的初始值。

    复制代码
    // Java code illustrating initialValue() method 
    import java.lang.*; 
    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(name); 
        start(); 
    } 
    
    public void run() 
    { 
        for (int i = 0; i < 2; i++) 
            System.out.println(getName() + " " + gfg.get()); 
    } 
    } 
    
    public class ThreadLocalDemo { 
    
    public static void main(String[] args) 
        { 
    
            NewThread t1 = new NewThread("quiz1"); 
            NewThread t2 = new NewThread("quiz2"); 
        } 
    } 
    复制代码

    输出:

    quiz2 14
    quiz1 15
    quiz1 15
    quiz2 14

     

    
    
posted @   我要去巴萨  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示