In a previous post, I wrote the usage and benefits of ThreadLocal based instance variables in concurrent applications. This seemingly innocent and fail-proof implementation will provide clear data separation and visibility between threads in multi-threaded applications UNTIL, you use Thread Pooling.
 
ThreadLocal variables as the name suggest is local to the thread, til the thread is alive the ThreadLocal instance variable can not be Garbage Collected. This post explains the theory clearly.
 
To see it in action I wrote a small program which infinitely creates Tasks in one implementation submitting tasks to a Thread Pool and in another instance creating ad-hoc Threads which are no pooled. And I used jVisualVM to monitor the VM.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ThreadLocalTest { private static ExecutorService executor = Executors.newFixedThreadPool(100); public static void main(String[] args) {
while(true) {
// Thread Pooled based implementation
executor.execute(new SimpleThread());
// Ad-hoc Thread based implementation
new SimpleThread().start();
}
} public static class SimpleThread extends Thread { private ThreadLocal<Integer> no = new ThreadLocal<Integer>() { @Override
protected Integer initialValue() {
return 0;
} }; public void run() {
while(no.get() < 100) {
no.set(no.get() + 1);
}
System.out.println(String.format("Thread : %s Finished", Thread.currentThread().getName()));
no.remove();
}
}
}
In the Thread pooled implementation one thread throwing an Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "Thread Name" is just a matter of time.
 
Where as the adhoc Thread creation seems to keep on running with good VM Garbage Collection but the Thread pool implementation fills up the heap space within minutes and eventually runs out of memory. 
 
Lesson
 
Always keep an eye on the Thread classes you use in your application, make sure they don't have ThreadLocal variables when you are using Thread Pooling.

Give special care when you get pre-compiled third-party libraries which might have ThreadLocal so always read javadocs thoroughly when you use Thread pooling.

转载自:http://shazsterblog.blogspot.com/2015/01/anti-pattern-threadlocal-variables-with.html

Anti Pattern - ThreadLocal variables with Thread Pool(转)的更多相关文章

  1. The CLR's Thread Pool

    We were unable to locate this content in zh-cn. Here is the same content in en-us. .NET The CLR's Th ...

  2. Improve Scalability With New Thread Pool APIs

    Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...

  3. mysql thread pool

    转自:http://blog.csdn.net/wyzxg/article/details/8258033 mysql 线程处理流程图: Mysql支持单线程和多线程两种连接线程模式,如果单线程,则在 ...

  4. Thread pool引起的程序连接数据库响应慢

    数据库版本:percona-mysql 5.6.16 ​在很长一段时间,都会出现程序连接数据库,出现响应慢的情况,正常在几到几十毫秒之间,但是偶尔会出现上百毫秒的情况: 开始由于开发重新设置并调整过程 ...

  5. Reporting Service 告警"w WARN: Thread pool pressure. Using current thread for a work item"

    如果Reporting Service偶尔出现不可访问或访问出错情况,这种情况一般没有做监控的话,很难捕捉到.出现这种问题,最好检查Reporting Service的日志文件. 今天早上就遇到这样一 ...

  6. MySQL thread pool【转】

    本文来自:http://blog.chinaunix.net/uid-26896862-id-3993773.html 刚刚经历了淘宝的双11,真实感受到了紧张的氛围.尽管DB淡定的度过,但是历程中的 ...

  7. worksteal thread pool

    worksteal的场景 对于一个线程池,每个线程有一个队列,想象这种场景,有的线程队列中有大量的比较耗时的任务堆积,而有的线程队列却是空的,现象就是有的线程处于饥饿状态,而有的线程处于消化不良的状态 ...

  8. CLR thread pool

    Thread Pooling https://msdn.microsoft.com/en-us/library/windows/desktop/ms686756(v=vs.85).aspx Threa ...

  9. MySQL Thread Pool: Problem Definition

    A new thread pool plugin is now a part of the MySQL Enterprise Edition.In this blog we will cover th ...

随机推荐

  1. python根据字典的值进行排序:

    有一个列表嵌套字典:[{"a": 5}, {"b": 4}, {"c": 1},{"e": 2}, {"d&q ...

  2. [PKUSC2018]主斗地

    暴搜 非常暴力的搜索,以至于我都不相信我能过. 方法是:暴力枚举所有牌型,然后暴力判断是否可行. 暴力枚举部分: 非常暴力: void dfs(int x,int l){ if(l==0){ flag ...

  3. Ubuntu 16.04LTS 安装和配置Bochs

    环境:VMWare14+Ubuntu16.04 安装Bochs2.6.9 1.去官网下载 下载 bochs-2.6.9.tar.gz 2.安装一系列的包 因为Bochs 需要在 X11 环境下运行,因 ...

  4. HTML 009 select

    本篇文章并非描述HTML中的select标签, 而是描述JSP中的<s:select> 关于HTML中的select标签, 以及和JSP中的<s:select>的相同以及差异后 ...

  5. Linux sudo权限绕过(CVE-2019-14287)

    2019年10月14日,Sudo官方发布了Sudo 1.8.28版本,其中包含sudo root权限绕过漏洞的补丁修复. 此漏洞编号是CVE-2019-14287,当sudo配置为允许用户以任意方式运 ...

  6. H3CNE学习5 STP

    一.STP 1.概念 2.STP开机默认会运行 二.STP操作 1.原理 2.根桥选举,首先比前面的ID,谁小谁就是根桥,如果ID一样就比较mac,谁小谁就是根桥 可以手动修改优先级,图中可以将swA ...

  7. WinDbg的安装、配置和功能

    一.WinDbg简介 WinDbg是微软发布的一款免费而十分强大的调试工具.既然是微软自己发布的调试工具,那它对微软产品的调试当然是十分的强大.Windows 调试器 (WinDbg) 可用于调试内核 ...

  8. 记下mongoose(转载)

    连接mongodb时使用的是mongoose模块,安装和使用方法如下: 安装: npm install mongoose --save 使用: let mongoose = require('mong ...

  9. 【mysql】连接和断开服务器

    [mysql]连接和断开服务器 #启动服务: $sudo service mysql start #停止服务: $sudo service mysql stop 要连接到服务器,我们通常需要提供MyS ...

  10. A tow-day exam

    D1 T1l \(des:\) 给出一棵树,判断树上两条路径是否相交 \(sol:\) 判断其中一条路径的两个端点以及两端点的 \(lca\) 是否存在于另一条链上 由于这是一棵树,任一点为根后这样判 ...