Executor框架是指java 5中引入的一系列併發庫中與executor相關的一些功能類,其中包括線程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等。他們的關係爲:
併發編程的一種編程方式是把任務拆分爲一些列的小任務,即Runnable,然後在提交給一個Executor執行,Executor.execute(Runnalbe)。Executor在執行時使用內部的線程池完成操作。
一、創建線程池
Executors類,提供了一系列工廠方法用於創先線程池,返回的線程池都實現了ExecutorService接口。
public static ExecutorService newFixedThreadPool(int nThreads)
創建固定數目線程的線程池。
public static ExecutorService newCachedThreadPool()
創建一個可緩存的線程池,調用execute 將重用以前構造的線程(如果線程可用)。如果現有線程沒有可用的,則創建一個新線程並添加到池中。終止並從緩存中移除那些已有 60 秒鐘未被使用的線程。
public static ExecutorService newSingleThreadExecutor()
創建一個單線程化的Executor。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
創建一個支持定時及週期性的任務執行的線程池,多數情況下可用來替代Timer類。
- Executor executor = Executors.newFixedThreadPool(10);
- Runnable task = new Runnable() {
- @Override
- public void run() {
- System.out.println("task over");
- }
- };
- executor.execute(task);
- executor = Executors.newScheduledThreadPool(10);
- ScheduledExecutorService scheduler = (ScheduledExecutorService) executor;
- scheduler.scheduleAtFixedRate(task, 10, 10, TimeUnit.SECONDS);
二、ExecutorService與生命週期
ExecutorService擴展了Executor並添加了一些生命週期管理的方法。一個Executor的生命週期有三種狀態,運行,關閉,終止。Executor創建時處於運行狀態。當調用ExecutorService.shutdown()後,處於關閉狀態,isShutdown()方法返回true。這時,不應該再想Executor中添加任務,所有已添加的任務執行完畢後,Executor處於終止狀態,isTerminated()返回true。
如果Executor處於關閉狀態,往Executor提交任務會拋出unchecked exception RejectedExecutionException。
- ExecutorService executorService = (ExecutorService) executor;
- while (!executorService.isShutdown()) {
- try {
- executorService.execute(task);
- } catch (RejectedExecutionException ignored) {
- }
- }
- executorService.shutdown();
三、使用Callable,Future返回結果
Future<V>代表一個異步執行的操作,通過get()方法可以獲得操作的結果,如果異步操作還沒有完成,則,get()會使當前線程阻塞。FutureTask<V>實現了Future<V>和Runable<V>。Callable代表一個有返回值得操作。
- Callable<Integer> func = new Callable<Integer>(){
- public Integer call() throws Exception {
- System.out.println("inside callable");
- Thread.sleep(1000);
- return new Integer(8);
- }
- };
- FutureTask<Integer> futureTask = new FutureTask<Integer>(func);
- Thread newThread = new Thread(futureTask);
- newThread.start();
- try {
- System.out.println("blocking here");
- Integer result = futureTask.get();
- System.out.println(result);
- } catch (InterruptedException ignored) {
- } catch (ExecutionException ignored) {
- }
ExecutoreService提供了submit()方法,傳遞一個Callable,或Runnable,返回Future。如果Executor後臺線程池還沒有完成Callable的計算,這調用返回Future對象的get()方法,會阻塞直到計算完成。
例子:並行計算數組的和。
- package executorservice;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- import java.util.concurrent.FutureTask;
- public class ConcurrentCalculator {
- private ExecutorService exec;
- private int cpuCoreNumber;
- private List<Future<Long>> tasks = new ArrayList<Future<Long>>();
- // 內部類
- class SumCalculator implements Callable<Long> {
- private int[] numbers;
- private int start;
- private int end;
- public SumCalculator(final int[] numbers, int start, int end) {
- this.numbers = numbers;
- this.start = start;
- this.end = end;
- }
- public Long call() throws Exception {
- Long sum = 0l;
- for (int i = start; i < end; i++) {
- sum += numbers[i];
- }
- return sum;
- }
- }
- public ConcurrentCalculator() {
- cpuCoreNumber = Runtime.getRuntime().availableProcessors();
- exec = Executors.newFixedThreadPool(cpuCoreNumber);
- }
- public Long sum(final int[] numbers) {
- // 根據CPU核心個數拆分任務,創建FutureTask並提交到Executor
- for (int i = 0; i < cpuCoreNumber; i++) {
- int increment = numbers.length / cpuCoreNumber + 1;
- int start = increment * i;
- int end = increment * i + increment;
- if (end > numbers.length)
- end = numbers.length;
- SumCalculator subCalc = new SumCalculator(numbers, start, end);
- FutureTask<Long> task = new FutureTask<Long>(subCalc);
- tasks.add(task);
- if (!exec.isShutdown()) {
- exec.submit(task);
- }
- }
- return getResult();
- }
- /**
- * 迭代每個只任務,獲得部分和,相加返回
- *
- * @return
- */
- public Long getResult() {
- Long result = 0l;
- for (Future<Long> task : tasks) {
- try {
- // 如果計算未完成則阻塞
- Long subSum = task.get();
- result += subSum;
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- public void close() {
- exec.shutdown();
- }
- }
Main
- int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11 };
- ConcurrentCalculator calc = new ConcurrentCalculator();
- Long sum = calc.sum(numbers);
- System.out.println(sum);
- calc.close();
四、CompletionService
在剛在的例子中,getResult()方法的實現過程中,迭代了FutureTask的數組,如果任務還沒有完成則當前線程會阻塞,如果我們希望任意字任務完成後就把其結果加到result中,而不用依次等待每個任務完成,可以使CompletionService。生產者submit()執行的任務。使用者take()已完成的任務,並按照完成這些任務的順序處理它們的結果。也就是調用CompletionService的take方法是,會返回按完成順序放回任務的結果,CompletionService內部維護了一個阻塞隊列BlockingQueue,如果沒有任務完成,take()方法也會阻塞。修改剛纔的例子使用CompletionService:
- public class ConcurrentCalculator2 {
- private ExecutorService exec;
- private CompletionService<Long> completionService;
- private int cpuCoreNumber;
- // 內部類
- class SumCalculator implements Callable<Long> {
- ......
- }
- public ConcurrentCalculator2() {
- cpuCoreNumber = Runtime.getRuntime().availableProcessors();
- exec = Executors.newFixedThreadPool(cpuCoreNumber);
- completionService = new ExecutorCompletionService<Long>(exec);
- }
- public Long sum(final int[] numbers) {
- // 根據CPU核心個數拆分任務,創建FutureTask並提交到Executor
- for (int i = 0; i < cpuCoreNumber; i++) {
- int increment = numbers.length / cpuCoreNumber + 1;
- int start = increment * i;
- int end = increment * i + increment;
- if (end > numbers.length)
- end = numbers.length;
- SumCalculator subCalc = new SumCalculator(numbers, start, end);
- if (!exec.isShutdown()) {
- completionService.submit(subCalc);
- }
- }
- return getResult();
- }
- /**
- * 迭代每個只任務,獲得部分和,相加返回
- *
- * @return
- */
- public Long getResult() {
- Long result = 0l;
- for (int i = 0; i < cpuCoreNumber; i++) {
- try {
- Long subSum = completionService.take().get();
- result += subSum;
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- public void close() {
- exec.shutdown();
- }
- }
五、例子HtmlRender
該例子模擬瀏覽器的Html呈現過程,先呈現文本,再異步下載圖片,下載完畢每個圖片即顯示,見附件eclipse項目htmlreander包。
所有代碼見附件,Eclipse項目。本文參考《Java併發編程實踐》。