如何优化 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 ...
随机推荐
- C# 微信开发-----微信会员卡(三)激活会员卡
在会员领取了会员卡之后需要做 一个跳转性激活,模式请看下图: 在创建会员卡的时候需要配置下这个参数的值: memberActivate.aspx页面代码如下: <%@ Page Language ...
- vs2010编译error_code
C1083 : 现象: xxxxx.cpp clxx:fatal error C1083:无法打开源文件: “..\..\..\..\src\folder1\folder2\folder3\folde ...
- linux服务器安装pyspide关于rgnutls.h: No such file or directory 的解决方案
In file included from src/docstrings.c:4:0: src/pycurl.h:148:30: fatal error: gnutls/gnutls.h: No su ...
- django第三天
从后台传入一层的字典数据 后台代码 前台代码一 浏览器结果 前台代码二 前台代码三 前台代码四 注意 默认遍历为KEY .values .keys .items 都不加括号
- 解决 RecyclerView 在Android Studio已经导入情况下还无法实例引用问题
系统:Windows 10 IDE::android studio 1. 问题:RecyclerView 在Android Studio已经导入情况下还无法实例引用问题 由于RecyclerView是 ...
- Eclipse中如何将项目转成Dynamic Web项目
项目接手前的同事是使用MyEclipse写的,拿SVN将项目导入eclipse时,web项目就会变成java项目,这时就需要修改配置文件将项目转成web,步骤如下: 1.修改项目配置文件. 要修改的项 ...
- 禁用大陆ip段
https://www.ip2location.com/free/visitor-blocker 有 .htaccess web.config等 以iis为例 打开C:\Windows\System3 ...
- 蓝桥杯 全球变暖(dfs)
标题:全球变暖 [题目描述]你有一张某海域NxN像素的照片,"."表示海洋."#"表示陆地,如下所示: 其中"上下左右"四个方向上连在一起的 ...
- HttpWebRequest简单使用
HttpWebRequest简单使用 摘要 HttpWebRequest类对WebRequest中定义的属性和方法提供支持,也对使用户能够直接与使用HTTP的服务器交互的附加属性和方法提供支持. 创 ...
- Oracle做insert或者update时未提交事务导致表锁定解决办法
//查看被锁定表有几个 select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_objects o ,v$sessi ...