轉自:https://blog.csdn.net/jdsjlzx/article/details/52912701
FutureTask既是Future、Runnable,又是包裝了Callable(如果是Runnable最終也會被轉換為Callable ), 它是這兩者的合體。
package io.renren.test2; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; public class test2 { public static void main(String[] args) throws ExecutionException, InterruptedException { testFuture(); // testCompletionService(); } //結果的輸出和線程的放入順序 有關(如果前面的沒完成,就算后面的哪個完成了也得等到你的牌號才能輸出!),so阻塞耗時 public static void testFuture() throws InterruptedException, ExecutionException { System.out.println("--1.1--> main Thread begin:"); ExecutorService executor = Executors.newCachedThreadPool(); List<Future<String>> result = new ArrayList<Future<String>>(); for (int i = 0; i < 10; i++) { Future<String> submit = executor.submit(new Task(i)); result.add(submit); } executor.shutdown(); for (int i = 0; i < 10; i++) {//一個一個等待返回結果 System.out.println("--1.2--> 一個一個等待返回結果: " + result.get(i).get()); } System.out.println("--1.3--> main Thread end:"); } //結果的輸出和線程的放入順序 無關(誰完成了誰就先輸出!主線程總是能夠拿到最先完成的任務的返回值,而不管它們加入線程池的順序),so很大大縮短等待時間 private static void testCompletionService() throws InterruptedException, ExecutionException { System.out.println("--2.2--> main Thread begin:"); ExecutorService executor = Executors.newCachedThreadPool(); ExecutorCompletionService<String> completionService = new ExecutorCompletionService<>(executor); for (int i = 0; i < 10; i++) { completionService.submit(new Task(i)); } executor.shutdown(); for (int i = 0; i < 10; i++) { // 檢索並移除表示下一個已完成任務的 Future,如果目前不存在這樣的任務,則等待。 Future<String> future = completionService.take(); //這一行沒有完成的任務就阻塞 System.out.println("--2.3--> " + future.get()); // 這一行在這里不會阻塞,引入放入隊列中的都是已經完成的任務 } System.out.println("--2.4--> main Thread end:"); } private static class Task implements Callable<String> { private volatile int i; public Task(int i) { this.i = i; } @Override public String call() throws Exception { Thread.sleep(1000); System.out.println(Thread.currentThread().getName()); return "--0.1--> 任務 : " + i; } } }
testFuture() 打印:
--1.1--> main Thread begin: pool-1-thread-9 pool-1-thread-2 pool-1-thread-1 pool-1-thread-3 pool-1-thread-6 pool-1-thread-7 pool-1-thread-5 pool-1-thread-8 pool-1-thread-4 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 0 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 1 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 2 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 3 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 4 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 5 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 6 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 7 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 8 pool-1-thread-10 --1.2--> 一個一個等待返回結果: --0.1--> 任務 : 9 --1.3--> main Thread end:
testCompletionService() 打印:
--2.2--> main Thread begin: pool-1-thread-1 --2.3--> --0.1--> 任務 : 0 pool-1-thread-2 pool-1-thread-7 --2.3--> --0.1--> 任務 : 1 --2.3--> --0.1--> 任務 : 6 pool-1-thread-3 --2.3--> --0.1--> 任務 : 2 pool-1-thread-8 --2.3--> --0.1--> 任務 : 7 pool-1-thread-9 --2.3--> --0.1--> 任務 : 8 pool-1-thread-6 pool-1-thread-5 pool-1-thread-4 --2.3--> --0.1--> 任務 : 5 --2.3--> --0.1--> 任務 : 4 --2.3--> --0.1--> 任務 : 3 pool-1-thread-10 --2.3--> --0.1--> 任務 : 9 --2.4--> main Thread end: