jdk1.7.0_79
## 在上一篇《ThreadPoolExecutor線程池原理及其execute方法》中提到了在執行緒池ThreadPoolExecutor的原理以及它的execute方法。本文解析ThreadPoolExecutor#submit。 對於一個任務的執行有時我們不需要它回傳結果,但是有我們需要它的回傳執行結果。對於執行緒來講,如果不需要它回傳結果則實作Runnable,而如果需要執行結果的話則可以實作Callable。在執行緒池同樣execute提供一個不需要傳回結果的任務執行,而對於需要結果傳回的則可呼叫其submit方法。 回顧ThreadPoolExecutor的繼承關係。 在Executor介面中只定義了execute方法,而submit方法則是在ExecutorService介面中定義的。//ExecutorServicepublic interface ExecutorService extends Executor { ... <T> Future<T> submit(Callable<T> task); <T> Future<T> submit(Runnable task, T result); <T> Future<T> submit(Runnable task); ... }
//AbstractExecutorServicepublic abstract class AbstractExecutorService implements ExecutorService { ... public <T> Future<T> submit(Callable<T> task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task); execute(ftask); return ftask; } public <T> Future<T> submit(Runnable task, T result) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task); execute(ftask); return ftask; } public Future<?> submit(Runnable task) { if (task == null) throw new NullPointerExeption(); RunnableFuture<Void> ftask = newTaskFor(task, null); execute(ftask); return ftask; } ... }
submit(Callable
package com.threadpoolexecutor;import java.util.concurrent.*;/** * ThreadPoolExecutor#sumit(Callable<T> task) * Created by yulinfeng on 6/17/17. */public class Sumit1 {public static void main(String[] args) throws ExecutionException, InterruptedException { Callable<String> callable = new Callable<String>() {public String call() throws Exception { System.out.println("This is ThreadPoolExetor#submit(Callable<T> task) method.");return "result"; } }; ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit(callable); System.out.println(future.get()); } }
# submit(Runnable task,# submit(Runnable task,
# submit(Runnable task, T result)
package com.threadpoolexecutor;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;/** * ThreadPoolExecutor#submit(Runnable task, T result) * Created by yulinfeng on 6/17/17. */public class Submit2 {public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); Data data = new Data(); Future<Data> future = executor.submit(new Task(data), data); System.out.println(future.get().getName()); } }class Data { String name;public String getName() {return name; }public void setName(String name) {this.name = name; } }class Task implements Runnable { Data data;public Task(Data data) {this.data = data; }public void run() { System.out.println("This is ThreadPoolExetor#submit(Runnable task, T result) method."); data.setName("kevin"); } }
submit(Runnable task)
#
package com.threadpoolexecutor;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;/** * ThreadPoolExecutor#sumit(Runnable runnables) * Created by yulinfeng on 6/17/17. */public class Submit {public static void main(String[] args) throws ExecutionException, InterruptedException { Runnable runnable = new Runnable() {public void run() { System.out.println("This is ThreadPoolExetor#submit(Runnable runnable) method."); } }; ExecutorService executor = Executors.newSingleThreadExecutor(); Future future = executor.submit(runnable); System.out.println(future.get()); } }
從上面的原始碼可以看到,這三者方法幾乎是一樣的,關鍵就在於:
###RunnableFuture<T> ftask = newTaskFor(task); execute(ftask);
//AbstractExecutorService#newTaskForprotected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { return new FutureTask<T>(callable); } protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask<T>(runnable, value); }
以上是ThreadPoolExecutor執行緒池之submit方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!