java 线程池问题

love_love 2015-10-28 08:41:49

public void execute(JobExecutionContext arg0) throws JobExecutionException {
ExecutorService pool = Executors.newCachedThreadPool(); //1
// ExecutorService pool = Executors.newFixedThreadPool(2); //2
for(int i=0 ; i<2 ;i++){
pool.execute(new ThreadListener());
}
System.out.println(pool.shutdownNow().size());
}



ThreadListener类

public class ThreadListener implements Runnable {



@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(new Date()+" "+Thread.currentThread().getName()+" 正在运行");
int i = 10/0;
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


请问大神两个问题:
1. 1标记的无界自动回收线程和2固定大小线程数为2 ,两者在此处应该是一样的吧。哪个对此处稍好点?
2. 在出现异常时,线程退出,是否可以被 垃圾回收机制回收。会存在内存溢出的问题吗?
...全文
193 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dracularking 2015-10-30
  • 打赏
  • 举报
回复
引用 4 楼 woxinfeixian93 的回复:
线程不是异常了 10/0 然后这线程就算执行为了,异常退出吧。
为了验证这个问题,异常发生后可以查看线程池中还有多少线程的
love_love 2015-10-29
  • 打赏
  • 举报
回复
线程不是异常了 10/0 然后这线程就算执行为了,异常退出吧。
  • 打赏
  • 举报
回复
你线程并没有退出啊 stop方法
love_love 2015-10-29
  • 打赏
  • 举报
回复
出现异常,原来的线程是直接被退出?然后加入一个新的线程进来吗?

Exception in thread "pool-8-thread-2" 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
Thu Oct 29 08:18:04 CST 2015 pool-8-thread-2 正在运行
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
java.lang.ArithmeticException: / by zero
	at com.yongan.quartz.ThreadListener.run(ThreadListener.java:17)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Thu Oct 29 08:18:06 CST 2015 pool-9-thread-1 正在运行
Thu Oct 29 08:18:06 CST 2015 pool-9-thread-2 正在运行
Exception in thread "pool-9-thread-1" java.lang.ArithmeticException: / by zero
Exception in thread "pool-9-thread-2" 	at com.yongan.quartz.ThreadListener.run(ThreadListener.java:17)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
0
java.lang.ArithmeticException: / by zero
	at com.yongan.quartz.ThreadListener.run(ThreadListener.java:17)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Thu Oct 29 08:18:08 CST 2015 pool-10-thread-1 正在运行
Exception in thread "pool-10-thread-1" java.lang.ArithmeticException: / by zero
	at com.yongan.quartz.ThreadListener.run(ThreadListener.java:17)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Thu Oct 29 08:18:08 CST 2015 pool-10-thread-2 正在运行
我打印出来的线程池名,都是在增加。我担心原来的没退出,还在线程池中?
dracularking 2015-10-28
  • 打赏
  • 举报
回复
1.一样的,总共两个线程 2. jvm退出后就都回收了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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