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. 依赖注入(DI)和控制反转(IOC)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  2. matlab运行程序时出现failed to start a parallel pool

    运行matlab做并行时得到如下报错: failed to start a parallel pool. (For information in addition to the causing err ...

  3. js获取当前时间往后加6天

    获取当前时间往后加6天,并绑定星期几(星期几是最笨的的方法,一个一个判读),后期在优化 <!DOCTYPE html> <html lang="en"> & ...

  4. Kubernetes 学习20调度器,预选策略及优选函数

    一.概述 1.k8s集群中能运行pod资源的其实就是我们所谓的节点,也称为工作节点.master从本质上来讲,他其实是运行整个集群的控制平面组件的比如apiserver,scheal,controlm ...

  5. kubefuse 让Kubernetes 资源成为fuse 文件系统

    kubefuse 是基于fuse 开发的文件系统,我们可以像访问文件系统一样访问Kubernetes 资源,使用python开发 支持以下特性: 可以使用方便的linux tools: ls. vim ...

  6. mysql 运算操作符

    Name Description AND, && Logical AND = Assign a value (as part of a SET statement, or as par ...

  7. HDU-盐水的故事

    http://acm.hdu.edu.cn/showproblem.php?pid=1408 这是一道高精度问题: 在自己错了数十遍之后找到了不少规律: 首先是Output limit exceede ...

  8. 在Matlab中的plot

    在Matlab中画图时,经常会使用plot函数命令.一些隐含的画图命令也默认调用plot函数命令,如spy命令,可以画出矩阵的稀疏度图形. 另外,有时为了方便,会将一些处理图形的操作和命令列在一块,命 ...

  9. 1045 Favorite Color Stripe (30)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

  10. Pytest权威教程15-运行Nose用例

    目录 运行Nose用例 使用方法 支持的nose风格 不支持的习语/已知问题 返回: Pytest权威教程 运行Nose用例 Pytest基本支持运行Nose框架格式的测试用例. 使用方法 后安装py ...