Java interface ExecutorService -- Practice.

前几天, 写完一个幕后扫描程序, 由于业务存在多对多的站点关系, 为了程序逻辑简单化, 牺牲掉一些硬件资源 来动态创建N个线程池(是池子哦).

结果悲剧发现: 公司服务器资源耗尽, 其它应用都跑不了, 系统宕机, 运维同事用不了命令.
幸好, 幸好, 有经验丰富的压力测试的同事帮顶住了, 结果2分钟解决BUG, 哈哈!
题外话, 技术组人员配置一定要有经验丰富的~

1.程序日志, 没有任何异常, 因为都是些普通业务, JMX 重型编程 没有在本程序中使用.
2.系统日志:
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /net failed
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /net failed
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /net failed
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /net failed
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /net failed
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /net failed
automount[4228]: expire_proc: expire thread create for /misc failed
automount[4228]: expire_proc: expire thread create for /net failed
.......................
fork: Resource temporarily unavailable


一看 fork. 我就直接想起 fork/join 并发编程的模型, 也不知道两者是不是有直接关系.

同事, 提醒线程太多 撑爆了系统!
我和同事都认为 Java GC 会[b]直接[/b]回收ExecutorService 对象啊! 事实相反, thread 一多也是很耗资源!
Java Doc :
/*
* An unused ExecutorService should be shut down to allow reclamation of its resources....
* @since 1.5
* @author Doug Lea
*/


程序结构,修改BUG后:

public class ChecksumsCalculatorServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
AsynService calculator = new AsyncServiceImpl(siteId, nodeName, someInfos,
requestCallbackImpl);
Thread t = new Thread(calculator);
t.start();
}
}

public class AsyncChecksumsServiceImpl implements AsyncChecksumsService {
private ExecutorService threadPool; // 悲剧就在这.
//init in construct.
public void run() {
try{
// threadPool do something
// get Future
}catch(...){
}finally{
/**let GC do its work*/
threadPool.shutdown();
threadPool = null;
}
}
}


[b]总结:
大资源由原来的三个类型 Stream , Connection, GUI 图形对象,
现在增加一个类型interface ExecutorService,
此四类都要用完都要关闭资源!
[/b]
悲剧源自数据量巨大, GC 不能回收用完的池子, 实战大数据并发编程才开始不久!
经验不足, 只能救助于书了!
并发编程推荐, 读英文好!
[size=large][b]Addison.Wesley.Java.Concurrency.in.Practice.May.2006.[/b][/size]


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
有应聘"Java工程师/架构师", 请发邮件至: linzuxiong1988@gmail.com, 并取得联系.
工程师招聘,岗位见: [url]http://job.youzan.com[/url] 请联系我: linzuxiong1988@gmail.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值