方法定义:
// ExecutorService接口中定义的方法:
Future<?> submit(Runnable task);
<T> Future<T> submit(Runnable task, T result);
<T> Future<T> submit(Callable<T> task);
// ThreadPoolExecutor中定义的方法:
public void execute(Runnable command)
submit方法和execute方法都可以用来提交任务给线程池去执行,但是两者有一些区别,如下:
1、定义方法的类不同
submit是在ExecutorService接口中定义的,而execute方法是在ThreadPoolExecutor类中定义的。
2、返回值类型不同
execute方法返回值为空,submit方法会以Future的形式返回线程的执行结果。
3、对异常的处理方式不同
如果执行的任务中产生了异常,execute方法会直接打印产生的异常的堆栈,由于该异常是在子线程中产生的,主线程中包围在execute方法周围的try-catch语句并不能捕获该异常。
而submit方法提交的子线程如果产生了异常,当调用submit方法返回的Future实例的get方法时,可以在主线程中通过try-catch捕获该异常。这里需要注意的是,如果不调用Future示例的get方法,是不能捕获到异常的。
示例:
public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 5, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
Runnable runnable = new Runnable() {
public void run() {
System.out.println("begin run");
Integer.parseInt("a");
System.out.println("finish run");
}
};
// 测试外层的try catch能否捕获execute方法抛出的异常
try {
System.out.println("begin execute");
pool.execute(runnable);
System.out.println("finish execute");
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch e from execute");
}
// 测试不执行submit方法返回的future的get方法时,外层的try catch能否捕获异常
try {
System.out.println("begin submit without get future");
Future future = pool.submit(runnable);
System.out.println("finish submit without get future");
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch e without get future");
}
// 测试执行submit方法返回的future的get方法时,外层的try catch能否捕获异常
try {
System.out.println("begin submit with get future");
Future future = pool.submit(runnable);
future.get();
System.out.println("finish submit with get future");
} catch (Exception e) {
e.printStackTrace();
System.out.println("catch e with get future");
}
}
执行结果:
Exception in thread "pool-1-thread-1" begin execute
finish execute
begin submit without get future
finish submit without get future
begin submit with get future
begin run
begin run
java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)begin run
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.nanxs.test.Test03$1.run(Test03.java:15)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.ExecutionException: java.lang.NumberFormatException: For input string: "a"
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.nanxs.test.Test03.main(Test03.java:41)
Caused by: java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.nanxs.test.Test03$1.run(Test03.java:15)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
catch e with get future
结论:
- execute方法执行线程,线程产生的异常在execute方法外面并不能捕获到异常,因为方法执行是异步的,try catch只能捕获当前线程执行产生的异常;
- submit方法执行线程,得到一个future,如果不去尝试获取future的内容,不会有异常抛出,原因同上一条;
- 实际上,future的生成是瞬时, 相当于得到一个占位符,具体的操作要去调用future的API才会执行;
- submit方法执行线程得到的future,如果调用这个future的API去获取结果,例如future.get(),如果线程中有异常产生,就可以通过在submit方法周围环绕try...catch来捕获这个异常。这是因为Future.get方法将线程执行的异常转化成ExecutionException,并在调用Future.get的线程中抛出。这部分实现在FutureTask,可以从FutureTask.get作为入口阅读源码