zoukankan      html  css  js  c++  java
  • Anti Pattern

    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

  • 相关阅读:
    龙小树|第一篇博客随笔
    机器学习相关网址
    希腊字母表
    博客园美化
    论文检索常用网站
    这些年,我用过的良心网站,分享给大家
    MATLAB小函数:展示灰度图像数据集的部分样例
    基于图嵌入的高斯混合变分自编码器的深度聚类(Deep Clustering by Gaussian Mixture Variational Autoencoders with Graph Embedding, DGG)
    MATLAB实例:二维散点图
    MATLAB实例:多元函数拟合(线性与非线性)
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11846627.html
Copyright © 2011-2022 走看看