今天创建了线程池,创建时候阿里巴巴提示我: 手动创建线程池,效果会更好哦。我也没在意。果然就遭到了报应,内存泄漏了…
本来我是这样创建的:
ExecutorService executorService = Executors.newFixedThreadPool(maxThreadNums);
executorService .execute(new Runnable() {
@Override
public void run() {
try {
// 逻辑代码
} catch (Exception e) {
e.printStackTrace();
}
}
});*/
然后改成了手动创建:
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
ExecutorService service = new ThreadPoolExecutor(5,5,200L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue(),namedThreadFactory);
service.execute(new Runnable() {
@Override
public void run() {
try {
// 逻辑代码
} catch (Exception e) {
e.printStackTrace();
}
}
});
具体为什么内存泄漏我也没深究,等有空了再去学习一下。