使用了java 线程池,程序会无端退出,求帮助!

javaprogramlover 2014-03-04 11:54:14
使用了java5带的concurrent包,创建了10一个线程池,运行了一段时间,程序没有任何征兆就退出了---不是死锁。而我把线程池的数降低到2个的时候,就不会出现程序退出的情况。求帮助。我是否需要自定义一个executorservice来在before 和after上监控到底发生了什么。
...全文
300 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
配置可以啊,你开10个线程,输入top -b -n 2,监控下cpu和内存的变化。
javaprogramlover 2014-03-05
  • 打赏
  • 举报
回复
引用 2 楼 fangmingshijie 的回复:
系统环境是什么样的?
AMD 3核心的 红帽子 linux,连接的是mySQL 数据库。10个消费线程从队列里往数据库保存数据,一个生产线程扫描磁盘!
javaprogramlover 2014-03-05
  • 打赏
  • 举报
回复
public class Broker { public ArrayBlockingQueue<FileWrapper> queue = new ArrayBlockingQueue<FileWrapper>( 4000); public Boolean continueProducing = Boolean.TRUE; public void put(FileWrapper data) throws InterruptedException { this.queue.put(data); } public FileWrapper get() throws InterruptedException { // return this.queue.take(); return this.queue.poll(3, TimeUnit.SECONDS); } }
javaprogramlover 2014-03-05
  • 打赏
  • 举报
回复
try { Broker broker = new Broker(); int consumerCount = 10; ExecutorService threadPool = Executors .newFixedThreadPool(consumerCount + 1); for (int i = 0; i < consumerCount; ++i) { DatabaseFactory.getInstance().saveAliasAs("consumer" + i); threadPool.execute(new Consumer("consumer" + i, broker)); } Future producerStatus = threadPool.submit(new Producer(broker)); // this will wait for the producer to finish its execution. producerStatus.get(); threadPool.shutdown(); threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (Exception e) { StackUtility.logStackTrace(log, e); }
晓风吹雾 2014-03-04
  • 打赏
  • 举报
回复
代码。。。。。。。。。。。。。。。。
大板牙花生 2014-03-04
  • 打赏
  • 举报
回复
你是不是在线程中加入中断了
  • 打赏
  • 举报
回复
系统环境是什么样的?
javaprogramlover 2014-03-04
  • 打赏
  • 举报
回复
加到100!!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧