Dubbo-Client线程池机制

2019-01-09  本文已影响0人  三云_16d2

DUbbo-Client默认的线程池是CachedThreadPool

/**
* 此线程池可伸缩,线程空闲一分钟后回收,新请求重新创建线程,来源于:<code>Executors.newCachedThreadPool()</code>
* 
* @see java.util.concurrent.Executors#newCachedThreadPool()
* @author william.liangf
*/
public class CachedThreadPool implements ThreadPool {

   public Executor getExecutor(URL url) {
       String name = url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME);
       int cores = url.getParameter(Constants.CORE_THREADS_KEY, Constants.DEFAULT_CORE_THREADS);
       int threads = url.getParameter(Constants.THREADS_KEY, Integer.MAX_VALUE);
       int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
       int alive = url.getParameter(Constants.ALIVE_KEY, Constants.DEFAULT_ALIVE);
       return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, 
               queues == 0 ? new SynchronousQueue<Runnable>() : 
                   (queues < 0 ? new LinkedBlockingQueue<Runnable>() 
                           : new LinkedBlockingQueue<Runnable>(queues)),
               new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url));
   }

}

实际过程中可能会导致client线程堆积

Downloads cat jstack.txt | grep "DubboClientHandler" | grep  "10.1.14.200" | wc -l
   1542
➜  Downloads cat jstack.txt | grep "DubboClientHandler" | grep  "10.1.14.200" | more
"DubboClientHandler-10.1.14.200:20880-thread-27" #12018 daemon prio=5 os_prio=0 tid=0x00007f40f1170000 nid=0x7b3a waiting on condition [0x00007f3de25e5000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #12008 daemon prio=5 os_prio=0 tid=0x00007f40e9283800 nid=0x7b24 waiting on condition [0x00007f3de2fef000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #12002 daemon prio=5 os_prio=0 tid=0x00007f40e5077800 nid=0x7b1e waiting on condition [0x00007f3de35f5000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11992 daemon prio=5 os_prio=0 tid=0x00007f40d5180000 nid=0x7b14 waiting on condition [0x00007f3de3fff000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11979 daemon prio=5 os_prio=0 tid=0x00007f40f9018800 nid=0x7b06 waiting on condition [0x00007f3de4d0c000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11973 daemon prio=5 os_prio=0 tid=0x00007f40f115c000 nid=0x7b00 waiting on condition [0x00007f3de5312000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11965 daemon prio=5 os_prio=0 tid=0x00007f40e9157000 nid=0x7af1 waiting on condition [0x00007f3de5b1a000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11955 daemon prio=5 os_prio=0 tid=0x00007f40e5065000 nid=0x780f waiting on condition [0x00007f3de6221000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11922 daemon prio=5 os_prio=0 tid=0x00007f40d5161800 nid=0x77ed waiting on condition [0x00007f3de8342000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11920 daemon prio=5 os_prio=0 tid=0x00007f40f9002800 nid=0x77eb waiting on condition [0x00007f3de8544000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11917 daemon prio=5 os_prio=0 tid=0x00007f40f1145800 nid=0x77e8 waiting on condition [0x00007f3de8847000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11912 daemon prio=5 os_prio=0 tid=0x00007f40e934d800 nid=0x77e1 waiting on condition [0x00007f3de8c4b000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11900 daemon prio=5 os_prio=0 tid=0x00007f40e504f000 nid=0x77cf waiting on condition [0x00007f3de9958000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11889 daemon prio=5 os_prio=0 tid=0x00007f40d5155000 nid=0x7532 waiting on condition [0x00007f3dea05f000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11888 daemon prio=5 os_prio=0 tid=0x00007f40f8ff7000 nid=0x7530 waiting on condition [0x00007f3dea160000]
"DubboClientHandler-10.1.14.200:20880-thread-27" #11884 daemon prio=5 os_prio=0 tid=0x00007f40f1139800 nid=0x7528 waiting on condition [0x00007f3dea463000]

写了个段测试代码

       ThreadPoolExecutor es = new ThreadPoolExecutor(40, 400, 1000, TimeUnit.MILLISECONDS,
               new SynchronousQueue<Runnable>(),
               new NamedThreadFactory("my-test", true));
       for (int i = 0; i < 400; ++i) {

           es.submit(new Runnable() {

               @Override
               public void run() {
                   System.out.println(System.currentTimeMillis());
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                   }
               }
           });
       }
       
       System.in.read();

上一篇下一篇

猜你喜欢

热点阅读