<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          為什么阿里不允許用Executors創(chuàng)建線程池

          共 10275字,需瀏覽 21分鐘

           ·

          2020-08-25 19:43

          1 文章概述

          《阿里巴巴JAVA開(kāi)發(fā)手冊(cè)》有這樣一條強(qiáng)制規(guī)定:線程池不允許使用Executors去創(chuàng)建,而應(yīng)該通過(guò)ThreadPoolExecutor方式,這樣處理方式更加明確線程池運(yùn)行規(guī)則,規(guī)避資源耗盡風(fēng)險(xiǎn)。本文我們從資源和排查問(wèn)題兩個(gè)角度進(jìn)行分析,同時(shí)參考DUBBO線程池聲明方式創(chuàng)建一個(gè)符合規(guī)范的線程池。


          2 資源角度

          《阿里巴巴JAVA開(kāi)發(fā)手冊(cè)》從資源角度對(duì)這個(gè)問(wèn)題進(jìn)行了分析

          FixedThreadPool SingleThreadPool允許請(qǐng)求隊(duì)列長(zhǎng)度為Integer.MAX_VALUE可能會(huì)堆積大量請(qǐng)求從而導(dǎo)致OOM
          CachedThreadPool ScheduledThreadPool允許創(chuàng)建線程數(shù)量為Integer.MAX_VALUE可能會(huì)創(chuàng)建大量線程從而導(dǎo)致OOM

          以下兩個(gè)線程池使用鏈表實(shí)現(xiàn)的阻塞隊(duì)列,不設(shè)大小理論上隊(duì)列容量無(wú)上限,所以可能會(huì)堆積大量請(qǐng)求從而導(dǎo)致OOM

          #?FixedThreadPool?SingleThreadPoolpublic static ExecutorService newFixedThreadPool(int nThreads) {    return new ThreadPoolExecutor(nThreads, nThreads,                                  0L, TimeUnit.MILLISECONDS,                                  new LinkedBlockingQueue());}
          public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()));}

          以下兩個(gè)線程池maxSize使用Integer最大值,所以可能會(huì)創(chuàng)建大量線程從而導(dǎo)致OOM

          # CachedThreadPool ScheduledThreadPoolpublic static ExecutorService newCachedThreadPool() {    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,                                  60L, TimeUnit.SECONDS,                                  new SynchronousQueue());}
          public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1));}
          public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue());}

          3 排查問(wèn)題角度

          如果使用Executors創(chuàng)建線程池,大家應(yīng)該最常使用如下語(yǔ)句

          public void testThread() throws Exception {    ExecutorService fixedExecutor = Executors.newFixedThreadPool(10);    for (int i = 0; i < 1000; i++) {        fixedExecutor.execute(new Runnable() {            @Override            public void run() {                System.out.println("公眾號(hào)互聯(lián)網(wǎng)公園");            }        });    }}

          上述語(yǔ)句在功能層面是沒(méi)有問(wèn)題的,但是在生產(chǎn)環(huán)境中有可能遇到CPU飆高,線程數(shù)持續(xù)增加,內(nèi)存溢出等問(wèn)題,我們時(shí)常需要通過(guò)線程快照進(jìn)行觀察。我們通過(guò)jstack命令觀察上述代碼線程快照

          "pool-1-thread-2" #525 prio=5 os_prio=0 tid=0x00006f6561039100 nid=0xdaa waiting on condition [0x00006f64e646d000]java.lang.Thread.State: WAITING (parking)at sun.misc.Unsafe.park(Native Method)parking?to?wait?for?<0x00000006e6f3e230>?(a?java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)at java.util.concurrent.locks.LockSupport.park(LockSupport.java:165)at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1126)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:616)at java.lang.Thread.run(Thread.java:645)

          我們發(fā)現(xiàn)從線程快照看不出任何業(yè)務(wù)信息,只有類似pool-1-thread-2這種編號(hào)信息,不利于排查問(wèn)題,我們需要給線程命名。


          4 為線程進(jìn)行命名

          在并發(fā)編程中我們一定要為線程命名,這樣有助于排查問(wèn)題,關(guān)于如何命名我們可以參考DUBBO源碼,分析FixedThreadPool線程池會(huì)發(fā)現(xiàn)其使用命名工廠為生產(chǎn)者和消費(fèi)者線程進(jìn)行命名

          public class FixedThreadPool implements ThreadPool {
          @Override public Executor getExecutor(URL url) {
          // 線程名稱 String name = url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME);
          // 線程個(gè)數(shù)默認(rèn)200 int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS);
          // 隊(duì)列容量默認(rèn)0 int queues = url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES);
          // 隊(duì)列容量等于0使用阻塞隊(duì)列SynchronousQueue // 隊(duì)列容量小于0使用無(wú)界阻塞隊(duì)列LinkedBlockingQueue // 隊(duì)列容量大于0使用有界阻塞隊(duì)列LinkedBlockingQueue // NamedInternalThreadFactory為線程命名 return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, queues == 0 ? new SynchronousQueue() : (queues < 0 ? new LinkedBlockingQueue() : new LinkedBlockingQueue(queues)), new NamedInternalThreadFactory(name, true), new AbortPolicyWithReport(name, url)); }}

          生產(chǎn)者默認(rèn)線程名DubboServerHandler

          public abstract class AbstractServer extends AbstractEndpoint implements Server {    protected static final String SERVER_THREAD_POOL_NAME = "DubboServerHandler";}

          生產(chǎn)者線程快照信息如下

          "DubboServerHandler-1.1.1.1:20881-thread-20"?#511?daemon?prio?=?5?os_prio?=?0?tid?=?0x00001f153121f200?nid?=?0xd1a?waiting?on?condition?[0x00001f14edcdf000]java.lang.Thread.State: WAITING (parking)at sun.misc.Unsafe.park(Native Method)- parking to wait for <0x00000001e1f3abc0> (a java.util.concurrent.SynchronousQueue$TransferStack)at java.util.concurrent.locks.LockSupport.park(LockSupport.java : 115)at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java : 452)at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java : 312)at java.util.concurrent.SynchronousQueue.take(SynchronousQueue.java : 924)at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java : 1011)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java : 1121)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java : 111)

          消費(fèi)者默認(rèn)線程名DubboClientHandler

          public abstract class AbstractClient extends AbstractEndpoint implements Client {    protected static final String CLIENT_THREAD_POOL_NAME = "DubboClientHandler";}

          消費(fèi)者線程快照信息如下

          "DubboClientHandler-1.1.1.1:20881-thread-10" #688 daemon prio=1 os_prio=0 tid=0x00001f6114004800 nid=0x14d8 waiting on condition [0x00001f63e131a000]java.lang.Thread.State: TIMED_WAITING (parking)at sun.misc.Unsafe.park(Native Method)-?parking?to?wait?for?<0x00000006e21df0d0>?(a?java.util.concurrent.SynchronousQueue$TransferStack)at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:111)at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:361)at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:141)at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1111)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:611)at java.lang.Thread.run(Thread.java:141)


          5 創(chuàng)建一個(gè)規(guī)范線程池

          我們參考DUBBO線程池定義命名工廠

          public class NamedInternalThreadFactory extends NamedThreadFactory {    public NamedInternalThreadFactory() {        super();    }
          public NamedInternalThreadFactory(String prefix) { super(prefix, false); }
          public NamedInternalThreadFactory(String prefix, boolean daemon) { super(prefix, daemon); }
          @Override public Thread newThread(Runnable runnable) { String name = mPrefix + mThreadNum.getAndIncrement(); InternalThread ret = new InternalThread(mGroup, runnable, name, 0); ret.setDaemon(mDaemon); return ret; }}
          public class NamedThreadFactory implements ThreadFactory { protected static final AtomicInteger POOL_SEQ = new AtomicInteger(1); protected final AtomicInteger mThreadNum = new AtomicInteger(1); protected final String mPrefix; protected final boolean mDaemon; protected final ThreadGroup mGroup;
          public NamedThreadFactory() { this("pool-" + POOL_SEQ.getAndIncrement(), false); }
          public NamedThreadFactory(String prefix) { this(prefix, false); }
          public NamedThreadFactory(String prefix, boolean daemon) { mPrefix = prefix + "-thread-"; mDaemon = daemon; SecurityManager s = System.getSecurityManager(); mGroup = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup(); }
          @Override public Thread newThread(Runnable runnable) { String name = mPrefix + mThreadNum.getAndIncrement(); Thread ret = new Thread(mGroup, runnable, name, 0); ret.setDaemon(mDaemon); return ret; }
          public ThreadGroup getThreadGroup() { return mGroup; }}

          再定義一個(gè)線程池,在線程池執(zhí)行方法開(kāi)放一個(gè)業(yè)務(wù)名稱參數(shù)供調(diào)用方設(shè)置

          public class ThreadPoolStarter {      public static ThreadPoolExecutor getExecutor(String threadName) {        if (executor == null) {          synchronized (ThreadPoolStarter.class) {            if (executor == null) {              int coreSize = Runtime.getRuntime().availableProcessors();              BlockingQueue queueToUse = new LinkedBlockingQueue(QUEUE_SIZE);              executor = new ThreadPoolExecutor(coreSize, POOL_CORE_SIZE, MAX_SIZE, TimeUnit.SECONDS, queueToUse, new NamedInternalThreadFactory(threadName, true), new AbortPolicyDoReport(threadName));            }      }    }    return executor;  }}
          public class ThreadExecutor { public static void execute(String bizName, Runnable job) { ThreadPoolStarter.getExecutor(bizName).execute(job); }
          public static Future sumbit(String bizName, Runnable job) { return ThreadPoolStarter.getExecutor(bizName).submit(job); }}

          編寫(xiě)一個(gè)實(shí)例進(jìn)行測(cè)試

          public void testThread() throws Exception {    for (int i = 0; i < 10000; i++) {      ThreadExecutor.execute("BizName", new Runnable() {        @Override        public void run() {          System.out.println("公眾號(hào)互聯(lián)網(wǎng)公園");        }      });      Thread.sleep(1000L);    }  }}

          再觀察線程快照可以清晰查看業(yè)務(wù)名

          "BizName-thread-8" #262 daemon prio=5 os_prio=0 tid=0x0000000023b5c000 nid=0x31d4 waiting on condition [0x000000003c0be000]java.lang.Thread.State: WAITING (parking)at sun.misc.Unsafe.park(Native Method)-?parking?to?wait?for?<0x00000006c35781f0>?(a?java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)


          6 文章總結(jié)

          本文首先介紹了《阿里巴巴JAVA開(kāi)發(fā)手冊(cè)》不允許使用Executors創(chuàng)建線程池這個(gè)規(guī)定,然后從資源和排查問(wèn)題兩個(gè)角度分析了為什么這么規(guī)定,最后我們參考DUBBO線程池聲明方式創(chuàng)建了一個(gè)規(guī)范線程池,這樣使用線程池有助于快速定位和排查問(wèn)題。


          長(zhǎng)按二維碼關(guān)注更多精彩文章

          瀏覽 31
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91精选欧美成人AAA片 | 一区色| 影音先锋成人在线视频 | 婷婷综合五月 | 在线看片肏 |