jdk1.7.0_79
## In the previous article "ThreadPoolExecutor thread The principle of the thread pool ThreadPoolExecutor and its execute method are mentioned in "Pool Principle and Its Execute Method". This article analyzes ThreadPoolExecutor#submit. For the execution of a task, sometimes we don’t need it to return results, but there are times when we need its return execution results. For a thread, if it does not need to return a result, it can implement Runnable, and if it needs to execute the result, it can implement Callable. In the thread pool, execute also provides a task execution that does not need to return a result, and its submit method can be called for those that need to return a result. Review the inheritance relationship of ThreadPoolExecutor. Only the execute method is defined in the Executor interface, while the submit method is defined in the ExecutorService interface.//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, 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); }
The above is the detailed content of Submit method of ThreadPoolExecutor thread pool. For more information, please follow other related articles on the PHP Chinese website!