execute和submit都属于线程池的方法,execute只能提交Runnable类型的任务,而submit既能提交Runnable类型任务也能提交Callable类型任务。
execute会直接抛出任务执行时的异常,submit会吃掉异常,可通过Future的get方法将任务执行时的异常重新抛出。
execute所属顶层接口是Executor,submit所属顶层接口是ExecutorService,实现类ThreadPoolExecutor重写了execute方法,抽象类AbstractExecutorService重写了submit方法。
submit和execute由于参数不同有四种实现形式,如下所示,本文主要研究这四种形式在各自使用场景下的区别和联系
- <T> Future<T> submit(Callable<T> task);
- <T> Future<T> submit(Runnable task, T result);
- Future<?> submit(Runnable task);
- void execute(Runnable command);
关于Runnable和Callable任务如果你还存在疑惑,建议你先看看我的上篇文章Runnable和Callable的区别和联系。
测试代码的整体框架如下:
- import java.util.concurrent.*;
-
- public class TestSubmitAndExecute {
- static ExecutorService executor = Executors.newCachedThreadPool();
-
- public static void main(String[] args) {
- initExecutors();
- /**put test codes here*/
-
-
- /***/
- waitToTerminated();
-
- }
-
-
- private static void initExecutors() {
- if (executor.isTerminated()) {
- executor = Executors.newCachedThreadPool();
- }
- }
-
- private static void waitToTerminated() {
- executor.shutdown();
- while (!executor.isTerminated()) {
- }
- }
-
- /**
- * 测试 submit(Callable<T> task)
- *
- * @param callable
- * @param <T>
- * @return
- */
- public static <T> T testSubmitCallable(Callable callable) {
- Future<T> future = executor.submit(callable);
- T result = null;
- try {
- result = future.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- return result;
- }
-
- /**
- * 测试submit(Runnable task, T result)
- *
- * @param runnable
- * @param t
- * @param <T>
- * @return
- */
- public static <T> T testSubmitRunnable(Runnable runnable, T t) {
- Future<T> future = executor.submit(runnable, t);
- T result = null;
- try {
- result = future.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- return result;
- }
-
- /**
- * 测试 submit(Runnable task)
- * submit提交Runnable任务会默认返回null
- *
- * @param runnable
- * @return
- */
- public static Object testSubmitRunnable(Runnable runnable) {
- Future<?> future = executor.submit(runnable);
- Object v = null;
- try {
- v = future.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- return v;
- }
-
- /**
- * 测试 execute(Runnable command)
- * execute会直接抛出异常,submit只有通过调用Future对象的get方法才能获取异常
- *
- * @param runnable
- */
- public static void testExecuteRunnable(Runnable runnable) {
- executor.execute(runnable);
- }
- }
这个测试框架提供了4个静态方法用来测试submit和execute总共包含的四种表现形式,除此之外提供initExecutors用于提前检测线程池是否终止,若终止则初始化,waitToTerminated方法用于关闭线程池,并阻塞到线程池终止为止。
除了测试框架之外提供了4个不同的任务,分别测试Callable和Runnable在抛异常时的表现形式。
- class CallableTask implements Callable<Integer> {
- @Override
- public Integer call() throws Exception {
- int sum = 0;
- for (int i = 0; i < 520; i++) {
- sum += i;
- }
- return sum;
- }
- }
-
- /**
- * 会抛异常的CallableTask
- */
- class ExceptionCallableTask implements Callable<Boolean> {
- public Boolean call() throws Exception {
- int num = 1 / 0;
- return false;
- }
- }
-
- class RunnableTask implements Runnable {
- @Override
- public void run() {
- System.out.println("I am a runnable task");
- }
- }
-
- /**
- * 会抛异常的RunnableTask
- */
- class ExceptionRunableTask implements Runnable {
- @Override
- public void run() {
- int num = 1 / 0;
- }
- }
整体结构搭起来,下来就是研究具体差异的时刻了。
1)首先研究Future<?> submit(Runnable task)和void execute(Runnable command),这两个方法都是执行Runnable类型任务,前者有返回值,但是返回值为null,后者无返回值。
- public static void main(String[] args) {
- initExecutors();
- /**put test codes here*/
- Object object = testSubmitRunnable(new RunnableTask());
- System.out.println(object);
-
- testExecuteRunnable(new RunnableTask());
-
- /***/
- waitToTerminated();
- }
很容易观察控制台输出如下:
- I am a runnable task
- null
- I am a runnable task
可以看出submit执行Runnable类型任务时默认返回值为null。如果我们需要submit在提交Runnable任务可以返回非空,就需要用到submit的另外一个重载的方法:<T> Future<T> submit(Runnable task, T result);
2)submit(Runnable task, T result) 方法可以使submit执行完Runnable任务后返回指定的返回值。
main方法如下:
- public static void main(String[] args) {
- initExecutors();
- /**put test codes here*/
- // Object object = testSubmitRunnable(new RunnableTask());
- // System.out.println(object);
- //
- // testExecuteRunnable(new RunnableTask());
-
- Integer i = testSubmitRunnable(new RunnableTask(), 3);
- System.out.println(i);
-
- Boolean bool = testSubmitRunnable(new RunnableTask(), true);
- System.out.println(bool);
-
- String str = testSubmitRunnable(new RunnableTask(), "你好吗");
- System.out.println(str);
-
-
- /***/
- waitToTerminated();
- }
控制台输出:
- I am a runnable task
- 3
- I am a runnable task
- true
- I am a runnable task
- 你好吗
可以看出我们输入的什么参数,任务执行完毕后就返回什么参数。
3)submit(Callable<T> task)这个方法没什么好说的,用来提交Callable类型任务,返回值由call方法决定。
main方法如下:
- public static void main(String[] args) {
- initExecutors();
- /**put test codes here*/
- // Object object = testSubmitRunnable(new RunnableTask());
- // System.out.println(object);
- //
- // testExecuteRunnable(new RunnableTask());
-
- // Integer i = testSubmitRunnable(new RunnableTask(), 3);
- // System.out.println(i);
- //
- // Boolean bool = testSubmitRunnable(new RunnableTask(), true);
- // System.out.println(bool);
- //
- // String str = testSubmitRunnable(new RunnableTask(), "你好吗");
- // System.out.println(str);
-
- Object o = testSubmitCallable(new CallableTask());
- System.out.println(o);
-
- /***/
- waitToTerminated();
- }
CallableTask的执行逻辑是计算0到520之间的所有整数之和,所以控制台输出:
134940
4)关于execute和submit遭遇异常的表现
execute直接将任务执行时期的异常抛出,main方法和控制台打印分别如下:
- public static void main(String[] args) {
- initExecutors();
- /**put test codes here*/
- // Object object = testSubmitRunnable(new RunnableTask());
- // System.out.println(object);
- //
- // testExecuteRunnable(new RunnableTask());
-
- // Integer i = testSubmitRunnable(new RunnableTask(), 3);
- // System.out.println(i);
- //
- // Boolean bool = testSubmitRunnable(new RunnableTask(), true);
- // System.out.println(bool);
- //
- // String str = testSubmitRunnable(new RunnableTask(), "你好吗");
- // System.out.println(str);
-
- // Object o = testSubmitCallable(new CallableTask());
- // System.out.println(o);
-
- testExecuteRunnable(new ExceptionRunableTask());
-
- /***/
- waitToTerminated();
- }
- Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
- at ExceptionRunableTask.run(TestRunnableAndCallable.java:38)
- 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)
submit比较特殊,如果没有通过Future.get来获取结算结果,则吃掉异常。先将测试方法稍做调整,修改成如下形式:
- /**
- * 测试 submit(Callable<T> task)
- *
- * @param callable
- * @param <T>
- * @return
- */
- public static <T> T testSubmitCallable(Callable callable) {
- Future<T> future = executor.submit(callable);
- T result = null;
- /*
- try {
- result = future.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- */
- return result;
- }
当我们在main方法添加如下代码时,控制台其实没有打印任何异常
- public static void main(String[] args) {
- initExecutors();
- /**put test codes here*/
- // Object object = testSubmitRunnable(new RunnableTask());
- // System.out.println(object);
- //
- // testExecuteRunnable(new RunnableTask());
-
- // Integer i = testSubmitRunnable(new RunnableTask(), 3);
- // System.out.println(i);
- //
- // Boolean bool = testSubmitRunnable(new RunnableTask(), true);
- // System.out.println(bool);
- //
- // String str = testSubmitRunnable(new RunnableTask(), "你好吗");
- // System.out.println(str);
-
- // Object o = testSubmitCallable(new CallableTask());
- // System.out.println(o);
-
- // testExecuteRunnable(new ExceptionRunableTask());
-
- testSubmitCallable(new ExceptionCallableTask());
-
- /***/
- waitToTerminated();
- }
如果将testSubmitCallable代码中被注释的部分取消注释,则可以看到异常信息如下:
- java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
- at java.util.concurrent.FutureTask.report(FutureTask.java:122)
- at java.util.concurrent.FutureTask.get(FutureTask.java:192)
- at TestSubmitAndExecute.testSubmitCallable(TestSubmitAndExecute.java:58)
- at TestSubmitAndExecute.main(TestSubmitAndExecute.java:28)
- Caused by: java.lang.ArithmeticException: / by zero
- at ExceptionCallableTask.call(TestRunnableAndCallable.java:20)
- at ExceptionCallableTask.call(TestRunnableAndCallable.java:18)
- 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)
关于execute和submit的简单研究到此结束,谢谢观看。
原文地址:https://www.jianshu.com/p/29610984f1dd