【发布时间】:2015-07-18 16:24:54
【问题描述】:
如果任何提交的线程抛出异常,它不会返回异常。
我想为我的项目编写一段代码,如果任何线程执行失败,它应该在那里抛出异常并且它应该停止所有正在运行和计划的线程。
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
Thread t = new Thread(new MyObject());
executorService.submit(t);
}
我是这样写MyObject的..,
public class MyObject implements Runnable {
public void run() {
throw new NullPointerException("Sample NullPointerException");
}
}
这对我的目标来说是正确的实现吗...????? 我想实现这个目标,请给我一些指点。
提前谢谢....!!
【问题讨论】:
标签: java multithreading exception-handling threadpool threadpoolexecutor