package thread; 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; /** * JdkDemo : TestExecutorService.java * * @date 2014-8-5 * @author Meng * @version * */ public class TestExecutorService1 { // 初始化线程池 static ExecutorService executorService = Executors.newFixedThreadPool(100); /** * @param args * @throws InterruptedException * @throws ExecutionException */ public static void main(String[] args) throws InterruptedException, ExecutionException { Future<?> future1, future2; // 1. 提交任务 runnable , future 获取线程执行结果 future1 = executorService.submit(new ConsumerThread1()); // 2. 提交任务 callable , future 获取线程执行结果 future2 = executorService.submit(new ConsumerThread2()); // 3. 提交任务 FutureTask FutureTask<String> task3 = new FutureTask<String>(new ConsumerThread3()); executorService.submit(task3); System.out.println("------------task1 执行结果------------"); // 此处因为调用了 get 方法 , 主线程会等待子线程完成 System.out.println(String.format("result [%s]", future1.get())); System.out.println("\n"); System.out.println("------------task2 执行结果------------"); System.out.println(String.format("result [%s]", future2.get())); System.out.println("\n"); System.out.println("------------task3 执行结果------------"); System.out.println(String.format("result [%s]", task3.get())); System.out.println("\n"); executorService.shutdown(); System.out.println("结束"); } } /** * 实现 Runnable 接口 , future 模式 无法获取返回结果 JdkDemo : * TestExecutorService.java * * @date 2014-8-5 * @author Meng * @version * */ class ConsumerThread1 implements Runnable { public void run() { System.out.println("task1 start"); for (int i = 0; i < 4; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println(this.getClass().toString() + "-->" + i); } System.out.println("task1 end"); } } /** * 实现 Callable 接口 , 可以抛出异常 , 可以存在返回值 JdkDemo : * TestExecutorService.java * * @date 2014-8-5 * @author Meng * @version * */ class ConsumerThread2 implements Callable<String> { public String call() throws Exception { System.out.println("task2 start"); for (int i = 0; i < 4; i++) { Thread.sleep(1000); System.out.println(this.getClass().toString() + "-->" + i); } System.out.println("task2 end"); return "done"; } } /** * 实现 Callable 接口 , 可以抛出异常 , 可以存在返回值 JdkDemo : * TestExecutorService.java * * @date 2014-8-5 * @author Meng * @version * */ class ConsumerThread3 implements Callable<String> { public String call() throws Exception { System.out.println("task3 start"); for (int i = 0; i < 4; i++) { Thread.sleep(1000); System.out.println(this.getClass().toString() + "-->" + i); } System.out.println("task3 end"); return "done"; } }

lyn520 LV3
2022年4月14日
2469095052 LV8
2021年12月30日
融会贯通 LV3
2021年12月9日
猴哥猴哥 LV12
2021年6月15日
yyblmxl LV1
2021年4月25日
花椒谢霆锋 LV8
2021年3月15日
zhenghongixin4065 LV9
2021年1月25日
lll111 LV16
2020年7月17日
liuyilin9608 LV15
2020年5月17日
风笑天 LV2
2020年5月10日