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; /** * @description 线程池 * LUKE * 2018年6月8日 上午9:58:17 */ public class CallableTest { public static void main(String[] args) throws InterruptedException, ExecutionException { Thread t1 = Executors.defaultThreadFactory().newThread(new Runnable() { public void run() { for(int i = 0; i < 10; i ++){ try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println("###### threadName=" + Thread.currentThread().getName() + ", num="+i); } System.out.println("########################################## 执行完毕 ##########################################"); } }); Thread t2 = Executors.defaultThreadFactory().newThread(new Runnable() { public void run() { for(int i = 0; i < 10; i ++){ try { Thread.sleep(2000); } catch (InterruptedException e) { } System.out.println("$$$$$$ threadName=" + Thread.currentThread().getName() + ", num="+i); } System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 执行完毕 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); } }); Thread t3 = Executors.defaultThreadFactory().newThread(new Runnable() { @Override public void run() { for(int i = 0; i < 10; i ++){ try { Thread.sleep(3000); } catch (InterruptedException e) { } System.out.println("^^^^^^ threadName=" + Thread.currentThread().getName() + ", num="+i); } System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 执行完毕 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); } }); Callable<String> t4 = new Callable<String>() { @Override public String call() throws Exception { for(int i = 0; i < 10; i ++){ try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println("###### threadName=" + Thread.currentThread().getName() + ", num="+i); } System.out.println("########################################## 执行完毕 ##########################################"); return "t4"; } }; Callable<String> t5 = new Callable<String>() { @Override public String call() throws Exception { for(int i = 0; i < 10; i ++){ try { Thread.sleep(2000); } catch (InterruptedException e) { } System.out.println("$$$$$$ threadName=" + Thread.currentThread().getName() + ", num="+i); } System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 执行完毕 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); return "t5"; } }; Callable<String> t6 = new Callable<String>() { @Override public String call() throws Exception { for(int i = 0; i < 10; i ++){ try { Thread.sleep(3000); } catch (InterruptedException e) { } System.out.println("^^^^^^ threadName=" + Thread.currentThread().getName() + ", num="+i); } System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 执行完毕 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); return "t6"; } }; ExecutorService pool = Executors.newCachedThreadPool(); // pool.execute(t1); // pool.execute(t2); // pool.execute(t3); // System.out.println("pool.shutdown() ..."); // pool.shutdown(); // System.out.println("运行结束... "); Future<String> t4Future = pool.submit(t4); Future<String> t5Future = pool.submit(t5); Future<String> t6Future = pool.submit(t6); System.out.println("============================================"); System.out.println(t4Future.get()); System.out.println(t5Future.get()); System.out.println(t6Future.get()); System.out.println("***********************************************"); // String future = t4Future.get() + t5Future.get() + t6Future.get(); // System.out.println(future); // String threadResult = "t4t5t6"; // if(threadResult.equals(future)){ // System.out.println("pool shutdown ..."); // pool.shutdown(); // } System.out.println("pool.shutdown() ..."); pool.shutdown(); System.out.println("运行结束..."); } }