Guava线程池踩坑记录

时间:2021-06-22 20:48:17

Guava

google的并行化框架,研究不深,在此不班门弄斧,送上一个链接

坑描述

其实说坑有点儿委屈google了,因为根本不是guava的问题,是我自己的问题。长话短说,线程池记得关闭,记得关闭,记得关闭啊喂!!!重要的事情说三遍!!!

  • 后果 : 线程池没关你知道有多严重么?你肯定想不到,我把我们api的dev和beta server统统搞挂了啊,是server挂了,不是service挂了啊,哭!!!

  • 细节:上代码

//max thread num : 20
final ExecutorService pool = Executors.newFixedThreadPool(20);
ListeningExecutorService service = MoreExecutors.listeningDecorator(pool);
// ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(20));
List<Callable<Map<String, Object>>> tasks;

List<ListenableFuture<Map<String, Object>>> futures = Lists.newArrayList();
for(Map<String, Object> inputApp : appList) {
    ListenableFuture queryApp = service.submit(new Callable() {
        public Map<String, Object> call() {
            Map<String, Object>skyaidQueryRet = queryApp(inputApp, fromRedis);
            return  skyaidQueryRet;
        }
    });
    futures.add(queryApp);
}

final ListenableFuture<List<Map<String, Object>>> resultsFuture = Futures.successfulAsList(futures);
try {
    // block until all tasks are done
    retList = resultsFuture.get();
} catch (Exception e) {
    e.printStackTrace();
    pool.shutdown();
}
pool.shutdown();//重点就是这行
  • 错误: 我一开始忘记加 pool.shutdown();这一句,直接导致的就是线程池中的task即使都执行完,资源并不会释放,最终回答道jvm的线程创建上限,导致不能创建任何新的线程,从而全线崩溃,甚至影响到了ssh到server都不能执行命令!

  • throw error:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.jfinal.aop.Invocation.invoke(Invocation.java:85)
    at ...//省略部分描述
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor99.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.jfinal.aop.Invocation.invoke(Invocation.java:71)
    ... 23 more
Caused by: java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at ...//省略部分描述
    at com.trendmicro.skyaid.controller.ArsController.v1(ArsController.java:35)
    ... 27 more

The end!