首页  >  文章  >  Java  >  Java 线程局部

Java 线程局部

WBOY
WBOY原创
2024-08-30 16:03:25541浏览

同一个线程可以读写的变量可以使用Java中的ThreadLocal类来创建,因此如果两个线程执行相同的代码并且该代码引用了相同的变量ThreadLocal,两个线程看不到ThreadLocal的这些变量,因此可以通过使用ThreadLocal类使代码成为线程安全的,而如果不使用ThreadLocal类和ThreadLocal类的实例,代码就不是线程安全的可以像使用 new 运算符创建 java 中任何其他类的实例一样创建。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

ThreadLocal 类在 Java 中如何工作?

线程局部变量是由Java中的java.lang.ThreadLocal类提供的。这些变量与普通变量不同,因此每个独立的、拥有的、拥有该变量副本并已初始化的线程都可以使用 get 或 set 方法来访问它们。这基本上是除了编写不可变类之外实现线程安全的其他方法之一。

不再共享对象,因此不需要同步,从而提高应用程序的性能和可扩展性。 Java中的ThreadLocal类扩展了类对象。局部变量的扩展,是对线程的一种限制,是由Java中的ThreadLocal类提供的。只有单个线程可以查看ThreadLocal。彼此的线程无法看到两个线程的ThreadLocal变量。 ThreadLocal变量属于私有字段,在类中是静态的,状态在线程内部维护。

Java中ThreadLocal的创建

ThreadLocal 实例的创建方式与使用 new 运算符创建任何其他 java 对象实例的方式类似。考虑以下语法在 Java 中创建 ThreadLocal:

private ThreadLocal variable_name = new ThreadLocal();

ThreadLocal 的创建对于一个线程只能创建一次。这个ThreadLocal内部的多个线程可以设置然后获取这些值,并且只有线程设置的值才能被自己看到。

在Java中获取ThreadLocal值

可以使用 ThreadLocal 的 get() 方法读取存储在 ThreadLocal 中的值。考虑以下语法来获取 Java 中的 ThreadLocal 值:

String variable_name = (String) threadLocal.get();

在 Java 中设置 ThreadLocal 值

ThreadLocal 创建后要存储在其中的值可以使用 set() 方法设置。考虑以下语法在 Java 中设置 ThreadLocal 值:

threadLocal.set("value_that_needs_to_be_set");

删除 Java 中的 ThreadLocal 值。

ThreadLocal 创建后设置的值可以被删除。可以使用 ThreadLocalremove() 方法删除 ThreadLocal 值。请考虑以下语法来删除在 Java 中设置的 ThreadLocal 值:

threadLocal.remove();

实现 Java ThreadLocal 的示例

以下是提到的示例:

示例#1

ThreadLocal类的get()方法和set()方法演示程序:

代码:

//a class called demo is defined
public class Demo
{
//main method is called
public static void main(String[] args)
{
//an instance of the ThreadLocal is created for a number
ThreadLocal<Number> local = new ThreadLocal<Number>();
//an instance of the ThreadLocal is created for a string
ThreadLocal<String> val = new ThreadLocal<String>();
//setting the first number value using set() method
local.set(100);
//obtaining the current thread's value which returns a number using get() method
System.out.println("The number value that was set is = " + local.get());
//setting the second number value using set() method
local.set(90);
//obtaining the current thread's value which returns a numberusing get() method
System.out.println("The number value that was set is = " + local.get());
//setting the first string value using set() method
val.set("Welcome to ThreadLocal");
//obtaining the current thread's value which returns a stringusing get() method
System.out.println("The string value that was set is = " + val.get());
//setting the second string value using set() method
val.set("Learning is fun");
//obtaining the current thread's value which returns a stringusing get() method
System.out.println("The string value that was set is = " + val.get());
}
}

输出:

Java 线程局部

说明:在上面的程序中,定义了一个名为demo的类。然后定义main方法。然后为某个数字创建 ThreadLocal 的实例。然后为字符串创建 ThreadLocal 的实例。然后使用 set() 方法设置第一个数值。然后使用 get() 方法获取当前线程的返回数字的值。然后使用 set() 方法设置第二个数值。然后使用 get() 方法获取当前线程的返回数字的值。然后使用 set() 方法设置第一个字符串值。然后使用 get() 方法获取当前线程的值,该值返回一个字符串。然后使用 set() 方法设置第二个字符串值。然后使用 get() 方法获取当前线程的值,该值返回一个字符串。程序的输出如上面的快照所示。

示例#2

Java程序演示ThreadLocal类的remove()方法:

代码:

//a class called demo is defined
public class Demo
{
//main method is called
public static void main(String[] args)
{
//an instance of the ThreadLocal is created for a number
ThreadLocal<Number> local = new ThreadLocal<Number>();
//an instance of the ThreadLocal is created for a string
ThreadLocal<String> val = new ThreadLocal<String>();
//setting the first number value using set() method
local.set(100);
//obtaining the current thread's value which returns a number using get() method
System.out.println("The first number value that was set is = " + local.get());
//setting the second number value using set() method
local.set(90);
//obtaining the current thread's value which returns a number using get() method
System.out.println("The second number value that was set is = " + local.get());
//setting the first string value using set() method
val.set("Welcome to ThreadLocal");
//obtaining the current thread's value which returns a string using get() method
System.out.println("The first string value that was set is = " + val.get());
//Using remove() method to remove the first string value that was set using set() method
val.remove();
//obtaining the current thread's value which returns a string using get() method
System.out.println("The first string value after using remove() method is = " + val.get());
//Using remove() method to remove the first number value and second number value that was set using set() method
local.remove();
//obtaining the current thread's value using get() method
System.out.println("The first number value and second number value after using remove() method is = " + local.get());
}
}

输出:

Java 线程局部

说明:在上面的程序中,定义了一个名为demo的类。然后定义main方法。然后为某个数字创建 ThreadLocal 的实例。然后为字符串创建 ThreadLocal 的实例。然后使用 set() 方法设置第一个数值。然后使用 get() 方法获取当前线程的返回数字的值。然后使用 set() 方法设置第二个数值。然后使用 get() 方法获取当前线程的返回数字的值。然后使用 set() 方法设置第一个字符串值。

然后使用remove()方法删除使用set()方法设置的第一个字符串值。然后使用get()方法获取当前线程的值,它返回null,因为我们删除了使用remove()方法设置的值。然后使用remove()方法删除使用set()方法设置的第一个数值和第二个数值。然后使用get()方法获取当前线程的值,它返回null,因为我们删除了使用remove()方法设置的值。程序的输出如上面的快照所示。

Java中ThreadLocal类的优点

  • ThreadLocal 类使多线程任务变得更容易,因为对象的状态不在线程之间共享。
  • 不需要同步,因为对象的状态不在线程之间共享。
  • ThreadLocal 避免了一个对象暴露给多个线程。

结论

在本教程中,我们通过编程示例及其输出来了解 ThreadLocal 类的定义、ThreadLocal 类的工作原理及其方法。

以上是Java 线程局部的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java Thread Pool下一篇:Association in Java