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