谈谈对ThreadLocal的理解

news/2024/7/8 3:16:08

谈谈对ThreadLocal的理解

ThreadLocal的数据结构是:

在Thread线程对象内部定义了一个ThreadLocalMap,通过ThreadLocal对象访问,访问时key是当前ThreadLocal对象,value保存当前线程想要保存的数据

ThreadLocalMap的数据结构:

哈希表,当产生哈希冲突时,采用“开放地址法”处理,就是当前下标发生哈希冲突时,会将当前下标作为0,向后重新计算下标

ThreadLocal的作用:

在当前线程范围内保存数据,可以在同一个线程执行期间,在不同的类和方法之间共享数据的场景

展示一个小案例说明在不同类中还是可以访问线程的ThreadLocal所保存的值

package Threadday5;

public class Test03 {
	
	public  static final ThreadLocal<String> threadlocal = new ThreadLocal<String>();
	public static void main(String[] args) throws InterruptedException {
		
		Thread thread1 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				//向threadlocal中存放当前线程想要保存的数据
				threadlocal.set("吕布");
				decth();
				//不同类的方法
				new nb().show();
			}
		},"线程1");
		
		
		Thread thread2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				threadlocal.set("貂蝉");
				decth();
				new nb().show();
			}
		},"线程2");
		
		thread1.start();
		thread2.start();
		
		thread1.join();
		thread2.join();
	}
	
	public static void decth() {
		//获取当前线程中所保存的数据
		System.out.println("Test03"+Thread.currentThread().getName()+":"+threadlocal.get());
	}
}

class nb {
	public static void show() {
		System.out.println("nb"+Thread.currentThread().getName()+":"+Test03.threadlocal.get());

	}
}

当在父子线程中,想要达到ThreadLocal数据的共享,就要使用InheritableThreadLocal保存数据

package Threadday5;

public class Test04 {
	//父子线程,使用InheritableThreadLocal保存数据
	public static final InheritableThreadLocal<String> threadlocal = new InheritableThreadLocal<String>();
	public static void main(String[] args) throws InterruptedException {
		//在main主线程中保存数据
		threadlocal.set("我本将心向明月");
		
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				threadlocal.get();
				System.out.println(Thread.currentThread().getName()+":"+threadlocal.get());
			}
		});
		
		thread.start();
		thread.join();
		System.out.println("over~");
	}
}

如何避免内存泄漏?

使用ThreadLocal做为ThreadLocalMap中的key,会避免内存泄漏,并且key被设计为WeakReference弱引用,可以及时被GC(垃圾回收机制)回收,避免内存泄漏。

引用关系:

  • 强引用:发生GC(垃圾回收),都不会回收
  • SoftReference软引用:内存不足时,发生GC(垃圾回收),会被回收
  • WeakReference弱引用:发生GC(垃圾回收),就会被回收
  • 虚引用:基本不存在

http://lihuaxi.xjx100.cn/news/2058220.html

相关文章

记录java.util.Map.getOrDefault()方法导致JSP编译失败异常:Unable to compile class for JSP

记录java.util.Map.getOrDefault()方法导致JSP编译失败异常:Unable to compile class for JSP; 一、报错信息&#xff1a; type Exception report message Unable to compile class for JSP: description The server encountered an internal error that prevented it from fu…

Making Dynamic Page Coalescing Effective on Virtualized Clouds——论文泛读

EuroSys 2023 Paper 论文阅读笔记整理 问题 在现代计算机系统中&#xff0c;转换后备缓冲器&#xff08;TLB&#xff09;容量不能与存储器容量相同的速率进行扩展[1&#xff0c;2]。地址转换开销已成为许多大内存工作负载的主要性能瓶颈[3-19]。在虚拟化云中&#xff0c;用硬…

Harbor 的安装及使用

Harbor 安装官网手册&#xff1a; https://goharbor.io/docs/2.10.0/install-config/download-installer/ Harbor 发布包地址&#xff1a; https://github.com/goharbor/harbor/releases 在部署harbor的前提下先安装docker 和 docker-compose 安装docker&#xff1a;https://d…

基于ssm课程管理系统

基于SSM的课程管理系统的设计与实现 摘 要 当下&#xff0c;正处于信息化的时代&#xff0c;许多行业顺应时代的变化&#xff0c;结合使用计算机技术向数字化、信息化建设迈进。以前学校对于课程信息的管理和控制&#xff0c;采用人工登记的方式保存相关数据&#xff0c;这种以…

表单验证、属性绑定(一个属性根据另一个属性有无进行操作)

表单验证 一个属性根据另一个属性有无进行操作&#xff08;属性绑定&#xff09; 1、问题描述 ​ 需求&#xff1a;表单里面后两个属性需要根据前面一个属性进行有无判断。如果前面属性没有输入值&#xff0c;则不需要进行操作&#xff1b;如果前面属性有输入值&#xff0c;则…

C#中处理文件的类以及应用程序配置文件简介

文章目录 一、C#中处理文件的类介绍二、应用程序配置文件应用程序配置文件的用途&#xff1a;如何使用配置文件&#xff1a; 一、C#中处理文件的类介绍 在C#中&#xff0c;处理文件的通用类是System.IO.File&#xff0c;它提供了一系列静态方法&#xff0c;可用于创建、复制、…

用stream流将list转为map

用stream流将list转为map 1、将list转为Map<Long, List> 按照spaceId分组&#xff0c;spaceId相同的为一组数据&#xff1a; List<BasEvaluationPriceResultDto> list new ArrayList(); Map<Long, List<BasEvaluationPriceResultDto>> priceResult…

一个类的名字后缀有Handler的都是有什么作用?

在Java中&#xff0c;类名后缀为"Handler"通常表示该类是一个处理器&#xff08;Handler&#xff09;。处理器是用来处理特定任务或事件的组件&#xff0c;通常在事件驱动的编程中使用。这种命名惯例在许多框架和库中都很常见&#xff0c;其中一些常见的用法包括&…