如何优化 ThreadPoolExecutor
一、想让线程池在初始化时就干活,而不是等到第一次提交任务时才创建线程,该怎么做?
/**
* Starts all core threads, causing them to idly wait for work. This
* overrides the default policy of starting core threads only when
* new tasks are executed.
*
* @return the number of threads started
*/
public int prestartAllCoreThreads() {
int n = 0;
while (addWorker(null, true))
++n;
return n;
}
二、tomcat 8 如何优化ThreadPoolExecutor
1. tomcat优化:ThreadPoolExecutor#execute 在抛出 RejectedExecutionException时, 仍然尝试给队列添加任务
/**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the <tt>Executor</tt> implementation.
* If no threads are available, it will be added to the work queue.
* If the work queue is full, the system will wait for the specified
* time and it throw a RejectedExecutionException if the queue is still
* full after that.
*
* @param command the runnable task
* @throws RejectedExecutionException if this task cannot be
* accepted for execution - the queue is full
* @throws NullPointerException if command or unit is null
*/
public void execute(Runnable command, long timeout, TimeUnit unit) {
submittedCount.incrementAndGet();
try {
super.execute(command);
} catch (RejectedExecutionException rx) {
if (super.getQueue() instanceof TaskQueue) {
final TaskQueue queue = (TaskQueue)super.getQueue();
try {
if (!queue.force(command, timeout, unit)) {
submittedCount.decrementAndGet();
throw new RejectedExecutionException("Queue capacity is full.");
}
} catch (InterruptedException x) {
submittedCount.decrementAndGet();
throw new RejectedExecutionException(x);
}
} else {
submittedCount.decrementAndGet();
throw rx;
} }
}
2. TaskQueue 继承 LinkedBlockingQueue, 重写了offer方法
也可以分析下tomcat线程池在防止内存溢出方面做的工作
tomcat的ThreadExecutor其中一个重要的作用,就是对线程的ThreadLocal缓存的变量进行清理;(待验证)
为什么ThreadLocal要进行清理呢?如果是一个简单的main函数的话,那么在这个主线程中使用ThreadLocal缓存在程序结束之后,自动就随着JVM退出而消亡了;
如果是开启的一个线程,这个线程中使用了ThreadLocal缓存,线程退出,这种情况这块内存仍旧会进行回收;但是,线程池的线程是重复利用的,
很有可能会在某处使用了ThreadLocal缓存,但是忘记remove掉了,这种在线程池中是很致命的
如何优化 ThreadPoolExecutor的更多相关文章
- Java 程序优化 (读书笔记)
--From : JAVA程序性能优化 (葛一鸣,清华大学出版社,2012/10第一版) 1. java性能调优概述 1.1 性能概述 程序性能: 执行速度,内存分配,启动时间, 负载承受能力. 性能 ...
- Netty实现高性能RPC服务器优化篇之消息序列化
在本人写的前一篇文章中,谈及有关如何利用Netty开发实现,高性能RPC服务器的一些设计思路.设计原理,以及具体的实现方案(具体参见:谈谈如何使用Netty开发实现高性能的RPC服务器).在文章的最后 ...
- HBase设计与开发性能优化(转)
本文主要是从HBase应用程序设计与开发的角度,总结几种常用的性能优化方法.有关HBase系统配置级别的优化,这里涉及的不多,这部分可以参考:淘宝Ken Wu同学的博客. 1. 表的设计 1.1 Pr ...
- 【Bugly干货】Android性能优化典范之多线程篇
本文涉及的内容有:多线程并发的性能问题,介绍了 AsyncTask,HandlerThread,IntentService 与 ThreadPool 分别适合的使用场景以及各自的使用注意事项,这是一篇 ...
- python高性能代码之多线程优化
以常见的端口扫描器为实例 端口扫描器的原理很简单,操作socket来判断连接状态确定主机端口的开放情况. import socket def scan(port): s = socket.socket ...
- HBase性能优化方法总结(转)
本文主要是从HBase应用程序设计与开发的角度,总结几种常用的性能优化方法.有关HBase系统配置级别的优化,这里涉及的不多,这部分可以参考:淘宝Ken Wu同学的博客. 1. 表的设计 1.1 Pr ...
- Android性能优化典范 - 第5季
这是Android性能优化典范第5季的课程学习笔记,拖拖拉拉很久,记录分享给大家,请多多包涵担待指正!文章共10个段落,涉及的内容有:多线程并发的性能问题,介绍了AsyncTask,HandlerTh ...
- Android多线程任务优化1:探讨AsyncTask的缺陷
AsyncTask还有别的缺陷,在生成listview的时候,如果adapter里面的count动态改变的话,不能使用AsyncTask,只能使用Thread+Handler,否则会出现如下错误 j ...
- tomcat 性能优化(转)
tomcat nginx默许的post大小限制 tomcat nginx默认的post大小限制执行大文件上传,或者,大数据量提交时,当提交的数据大小超过一定限制时,发现后台从request取值的代码r ...
随机推荐
- 数位dp-入门模板题 hdu2089
#include<bits/stdc++.h> using namespace std; ][],n,m; void init(){//dp[i][j]:i位的数,最高位是j dp[][] ...
- linux关于软件安装的博文
https://www.cnblogs.com/kundeg/archive/2018/03/06/7247934.html https://www.cnblogs.com/qiaozhoulin/p ...
- docker 部署mvc项目 <四>
一:部署方式 直接使用centos镜像,做一个镜像,此镜像制定端口号,在centos容器中安装jexus独立版,就可以了 docker run -d -p : -itd --name wds cent ...
- hibernate 保存报错 Hibernate operation: could not get next sequence value;
错误信息: [2017-09-28 18:51:38,854]-[org.hibernate.util.JDBCExceptionReporter:101] ERROR - Numeric Overf ...
- MNIST数字识别问题
摘自<Tensorflow:实战Google深度学习框架> import tensorflow as tf from tensorflow.examples.tutorials.mnist ...
- 快速搭建MQTT服务器(MQTTnet和Apache Apollo)
前言 MQTT协议是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分,http://mqtt.org/. MQTT is a machine-to-machine (M2M)/" ...
- GA:GA优化BP神经网络的初始权值、阈值,从而增强BP神经网络的鲁棒性—Jason niu
global p global t global R % 输入神经元个数,此处是6个 global S1 % 隐层神经元个数,此处是10个 global S2 % 输出神经元个数,此处是4个 glob ...
- zabbix环境安装搭建
一.Zabbix简介 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix由zabbix server与可选组件zabbix agent两部分组成. ...
- Java 整数型的进制间的互相转换
/** * 整数型, 进制间的互相转换 */ public class IntConversion { public static void main(String[] args) { int num ...
- Java读取文本文件中文乱码问题
最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中文就变成乱码.读取代码如下: List<String> lines=new ArrayList<Stri ...