zoukankan      html  css  js  c++  java
  • 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
      
      
  • 相关阅读:
    分类管理模块
    Java8新特性 集合的stream的map
    条件和分页查询
    工作常用系统汇总
    Dubbo简单环境搭建
    深入浅出 TCP/IP 协议栈
    非对称加密
    理解Cookie和Session机制
    jQuery学习
    数据库系列学习(六)-函数之数学函数
  • 原文地址:https://www.cnblogs.com/crelle/p/13689511.html
Copyright © 2011-2022 走看看