Java's ThreadLocals make certain things easy, but special care must be taken to make sure they are removed from threads when they are no longer needed. If ThreadLocals are not removed from threads, they can easily cause memory leaks, because threads can live much longer than the applications that are executed on a thread.

Consider a web application running inside a servlet container like Tomcat. Each request is handled by a thread from a thread pool. These threads can live well after you web app is undeployed from the server.

One of the easiest ways to get this wrong is to assume that ThreadLocal.get() returning null means that the ThreadLocal is not installed in the thread. This is not the case.

Each thread has a map of ThreadLocals, and when ThreadLocal.get() is called for the first time, it actually installs the ThreadLocalinto that ThreadLocal map for the current thread with the default value, which is usually null. You must still remove the ThreadLocal from the thread by calling ThreadLocal.remove().

As a rule of thumb, for ThreadLocals where the default value is null, if get() returns nullremove() should be called immediately.

I have written a class that does most of this for me:

/**
* A conservative {@link ThreadLocal} that removes itself from the thread
* whenever its value is {@code null}.
* @author Jesse Long
* @param <T> The type of the value stored in this {@link ThreadLocal}
*/
public abstract class ConservativeThreadLocal<T>
extends ThreadLocal<T>
{
/**
* Returns the current value of the variable, or {@code null} if it has not
* been set. This method takes care to remove this {@link ThreadLocal} from
* the thread if the value has not been set.
* @return the current value of the variable, or {@code null} if it has not
* been set.
*/
@Override
public final T get()
{
T t = super.get();
if (t == null){
remove();
}
return t;
} /**
* Returns {@code null}. This implementation always returns {@code null} so
* that this {@link ThreadLocal} implementation can use its absent on a
* thread as an indication that the value is not set.
* @return {@code null}
*/
@Override
protected final T initialValue()
{
return null;
} /**
* Returns the current value of the variable. If the value has not been
* set, and {@code create} is {@code true}, then the {@link #create()}
* method is called, the value of the {@link ThreadLocal} is set to the
* return value of {@link #create()}, and is returned from this method. If
* the value has not been set, and {@code create} is {@code false}, then
* this method behaves exactly the same as the {@link #get()} method.
*
* @return the current value of the variable, or the default value if
* {@code create} is {@code true}.
*
* @param create whether or not to set the default value if it has not yet
* been set.
*/
public T get(boolean create)
{
T t = get();
if (t == null && create){
t = create();
set(t);
}
return t;
} /**
* Sets the current value. If {@code value} is {@code null}, then this
* {@link ThreadLocal} is removed from the thread.
* @param value The value to set.
*/
@Override
public void set(T value)
{
if (value == null){
remove();
}else{
super.set(value);
}
} /**
* Returns the default value for this object.
* @return the default value for this object.
* @see #get(boolean)
*/
protected abstract T create();
}

How to correctly handle ThreadLocal.get() returning null的更多相关文章

  1. correctly handle PNG transparency in Win IE 5.5 & 6.

    function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var arVersion = ...

  2. How to use System.Diagnostics.Process correctly

    I’ve seen many a question on stackoverflow and other places about running a process and capturing it ...

  3. ThreadLocal以及内存泄漏

    ThreadLocal是什么 ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用Thr ...

  4. Java -- 基于JDK1.8的ThreadLocal源码分析

    1,最近在做一个需求的时候需要对外部暴露一个值得应用  ,一般来说直接写个单例,将这个成员变量的值暴露出去就ok了,但是当时突然灵机一动(现在回想是个多余的想法),想到handle源码里面有使用过Th ...

  5. ThreadLocal 原理

    ThreadLocal是什么 ThreadLocal是一个本地线程副本变量工具类.主要用于将私有线程和该线程存放的副本对象做一个映射,各个线程之间的变量互不干扰,在高并发场景下,可以实现无状态的调用, ...

  6. 多线程之ThreadLocal类

    深入研究java.lang.ThreadLocal类 0.前言 ThreadLocal(线程变量副本)Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量.采用空间换 ...

  7. ThreadLocal 工作原理、部分源码分析

    1.大概去哪里看 ThreadLocal 其根本实现方法,是在Thread里面,有一个ThreadLocal.ThreadLocalMap属性 ThreadLocal.ThreadLocalMap t ...

  8. JavaSe:ThreadLocal

    JDK中有一个ThreadLocal类,使用很方便,但是却很容易出现问题.究其原因, 就是对ThreadLocal理解不到位.最近项目中,出现了内存泄漏的问题.其中就有同事在使用ThreadLocal ...

  9. ThreadLocal 源码剖析

    ThreadLocal是Java语言提供的用于支持线程局部变量的类.所谓的线程局部变量,就是仅仅只能被本线程访问,不能在线程之间进行共享访问的变量(每个线程一个拷贝).在各个Java web的各种框架 ...

随机推荐

  1. (转)Eclipse开发Web项目

    1. 建立最简单的JSP和servlet http://wenku.baidu.com/link?url=bcf8iwB3E5_gjl46WfZAekQUWsps0-G3MAbbKz5totQcvmS ...

  2. Forward团队-爬虫豆瓣top250项目-设计文档

    组长地址:http://www.cnblogs.com/mazhuangmz/p/7603594.html 成员:马壮,李志宇,刘子轩,年光宇,邢云淇,张良 设计方案: 1.能分析HTML语言: 2. ...

  3. 模式PK:命令模式VS策略模式

    1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...

  4. Windows Phone 8.1不完全体验报告

    在Build 2014中,微软倾心打造的Windows Phone 8.1终于粉墨登场,会场掌声不断.在大会结束后一周,经过漫长的等待,终于等到了开发者预览的推送,迫不及待地体验这一跨时代的移动系统. ...

  5. [ 9.10 ]CF每日一题系列—— 186A模拟处理字符串

    Description: 跟你两个不相同的字符串,问你能否将第一个字符串任意两个字母交换一次使得两字符串相同,YES or NO Solution: 一维模拟就好了 #include <iost ...

  6. shell 命令 ls -a

    接手其他人的shell脚本时,遇到了一个"."开头的文件目录, ll 始终找不到. 咨询了一下,才知道,"."开头的是隐藏文件. 这时候用 ll -a 或 ls ...

  7. Android-WebView与本地HTML (Java调用--->HTML的方法)

    上一篇博客 Android-WebView与本地HTML (HTML调用-->Java的方法) 介绍了 JavaScript 调用--> Java中的方法,而此篇博客是介绍 Java 调用 ...

  8. 分形之拆分三角形(Split Triangle)

    前面讲了谢尔宾斯基三角形,它是不停地将一个三角形拆分三个与之相似的三角形.这一节给大家展示的图形是将一个等腰钝角三角形不停地拆分两个与之相似的三角形. 核心代码: static void SplitT ...

  9. 解决.net+steeltoe服务客户端被服务调用出现400BadRequst错误

    一直尝试用steeltoe的官方示例被调用,一直报400BadRequst错误,换用Java写了一个简单client服务,却能正常被调用. 百思不得其解,用了一晚上填坑,开始觉得是不是IP没绑定,服务 ...

  10. VS2015下安装Entity Framework Power Tools

    Entity Framework Power Tools是一个由EntityFramework开发小组提供的工具,它可以从现有数据库生成Fluent款式的Code First代码. 怀着激动的心情安装 ...