快速业务通道

深入浅出多线程(4)对CachedThreadPool OutOfMemoryError问题的一些想法 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-15

深入浅出多线程(4)对CachedThreadPool OutOfMemoryError问题的一些想法

时间:2011-09-07 BlogJava vincent

接系列3,在该系列中我们一起探讨一下CachedThreadPool。

线程池是Conncurrent包提供给我们的一个重要的礼物。使得我们没有必要维 护自个实现的心里很没底的线程池了。但如何充分利用好这些线程池来加快我们 开发与测试效率呢?当然是知己知彼。本系列就说说对CachedThreadPool使用的 一下问题。

下面是对CachedThreadPool的一个测试,程序有问题吗?

package net.blogjava.vincent; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class CachedThreadPoolIssue { /** * @param args */ public static void main(String[] args) { ExecutorService es = Executors.newCachedThreadPool(); for(int i = 1; i<8000; i++) es.submit(new task()); } } class task implements Runnable{ @Override public void run() { try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

如果对JVM没有特殊的设置,并在Window平台上,那么就会有一 下异常的发生:

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread   at java.lang.Thread.start0(Native Method)   at java.lang.Thread.start(Unknown Source)   at java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize (Unknown Source)   at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)   at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)   at net.blogjava.vincent.CachedThreadPoolIssue.main (CachedThreadPoolIssue.java:19)

深入浅出多线程(4)对CachedThreadPool OutOfMemoryError问题的一些想法(2)

时间:2011-09-07 BlogJava vincent

看看Doc对该线程池的介绍:

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.

有以下几点需要注意:

1. 指出会重用先前的线程,不错。

2. 提高了短Task的吞吐量。

3. 线程如果60s没有使用就会移除出Cache。

好像跟刚才的错误没有关系,其实就第一句话说了问题,它会按需要创建新 的线程,上面的例子一下提交8000个Task,意味着该线程池就会创建8000线程, 当然,这远远高于JVM限制了。

注:在JDK1.5中,默认每个线程使用1M内存,8000M !!! 可能吗!!

所以我感觉这应该是我遇到的第一个Concurrent不足之处,既然这么设计, 那么就应该在中Do

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号