Clean ThreadLocals
A method to clean ThreadLocal
   private void cleanThreadLocals() {
        try {
            // Get a reference to the thread locals table of the current thread
            Thread thread = Thread.currentThread();
            Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
            threadLocalsField.setAccessible(true);
            Object threadLocalTable = threadLocalsField.get(thread);
            // Get a reference to the array holding the thread local variables inside the
            // ThreadLocalMap of the current thread
            Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
            Field tableField = threadLocalMapClass.getDeclaredField("table");
            tableField.setAccessible(true);
            Object table = tableField.get(threadLocalTable);
            // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object
            // is a reference to the actual ThreadLocal variable
            Field referentField = Reference.class.getDeclaredField("referent");
            referentField.setAccessible(true);
            for (int i=0; i < Array.getLength(table); i++) {
                // Each entry in the table array of ThreadLocalMap is an Entry object
                // representing the thread local reference and its value
                Object entry = Array.get(table, i);
                if (entry != null) {
                    // Get a reference to the thread local object and remove it from the table
                    ThreadLocal threadLocal = (ThreadLocal)referentField.get(entry);
                    threadLocal.remove();
                }
            }
        } catch(Exception e) {
            // We will tolerate an exception here and just log it
            throw new IllegalStateException(e);
        }
    }
Clean ThreadLocals的更多相关文章
- Error:Execution failed for task ':app:clean'.
		运行时出现 Error:Execution failed for task ':app:clean'. 错误,Builld->Clean Project即可. 
- 学习Maven之Maven Clean Plugin
		1.maven-clean-plugin是个什么鬼? maven-clean-plugin这个插件用maven的人都不陌生.我们在执行命令mvn clean时调用的就是这个插件. 这个插件的主要作用就 ... 
- AndroidStudio中make Project、clean Project、Rebuild Project的区别
		1.Make Project:编译Project下所有Module,一般是自上次编译后Project下有更新的文件,不生成apk. 2.Make Selected Modules:编译指定的Modul ... 
- Clean Old Kernels on CentOS
		1. Check Installed Kernels $ rpm -q kernel 2. Clean Old Kernels ## need Install yum-utils ## ## Pack ... 
- 第3月第21天 nsclassfromstring返回null SVN报错:clean the working copy and then retry the operation
		1. xcodeproj工程损坏时,.m文件没有加入编译. 2. SVN报错:clean the working copy and then retry the operation http://bl ... 
- Creating a Clean, Minimal-Footprint ASP.NET WebAPI Project with VS 2012 and ASP.NET MVC 4
		Creating a Clean, Minimal-Footprint ASP.NET WebAPI Project with VS 2012 and ASP.NET MVC 4 Building O ... 
- TortoiseSVN Clean up 失败的处理方法
		当使用 TortoiseSVN 下载项目失败之后,重新下载之前需要 Clean up,在 TortoiseSVN 中 Clean up 总是失败. 在命令行行中执行 svn cleanup 就成功 ... 
- 设置Distribution clean up 每次删除Command的数量
		Replication Job “Distribution clean up: distribution” 默认设置是,每10minutes运行一次,每次删除2000个Command.这对于有1.9亿 ... 
- 转: GUI应用程序架构的十年变迁:MVC,MVP,MVVM,Unidirectional,Clean
		十年前,Martin Fowler撰写了 GUI Architectures 一文,至今被奉为经典.本文所谈的所谓架构二字,核心即是对于对于富客户端的 代码组织/职责划分 .纵览这十年内的架构模式变迁 ... 
随机推荐
- CodeSignal 刷题 —— matrixElementSum
			After they became famous, the CodeBots all decided to move to a new building and live together. The ... 
- webstrom中如何将npm菜单调出?
			在package.json文件上点击右键>>>点击show npm scripts就可以了.如图: 
- TF:Tensorflow结构简单应用,随机生成100个数,利用Tensorflow训练使其逼近已知线性直线的效率和截距—Jason niu
			import os os.environ[' import tensorflow as tf import numpy as np x_data = np.random.rand(100).astyp ... 
- Manjaro安装配置笔记
			简单介绍: Manjaro和Ubuntu的都使用有段时间了,还是AUR大法用着舒服趁着由KDE桌面更换deepin时系统崩溃,直接重装了系统,版本:Manjaro-deepin-17.1.7-x86_ ... 
- linux 学习笔记 防火墙设置
			1> 重新设置启动防火墙命令 #service iptables restart 2>添加防火墙规则命令 2.1 #service iptables stop 停止防火墙 2.2 #vi ... 
- SpringBoot应用War包形式部署到外部Tomcat
			这一篇文章介绍SpringBoot应用修改默认打jar形式部署为打war包形式,部署到外部Tomcat. SpringBoot应用默认打包成为可执行jar模式让我们感觉到部署的便捷,接下来给大家介绍一 ... 
- XamarinSQLite教程在Xamarin.Android项目中使用数据库
			XamarinSQLite教程在Xamarin.Android项目中使用数据库 在Xamarin.Android项目中使用预设数据库的具体操作步骤如下: (1)创建一个Xamarin.Android项 ... 
- angular笔记_6
			<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ... 
- ICPC Asia Regional 2015 Japan.Routing a Marathon Race(DFS)
			vjudge \(Description\) 给定一张\(n\)个点\(m\)条边的无向图,每个点有一个权值.求一条从\(1\)到\(n\)的路径,使得代价最小,输出最小代价. 一条路径的代价定义为, ... 
- Windows下bat脚本自动发邮件
			摘要:说明:很多木马会利用自身的程序截取系统敏感文件或信息发往指定的邮箱,而blat并不是木马,它小巧却有强大的发邮件功能,可不要用它做违法事,感觉和木马功能有一拼!下面先看个具体的实例(在blat同 ... 
