|
| 1 | +package cn.aofeng.demo.java.util.future; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.Callable; |
| 6 | +import java.util.concurrent.CancellationException; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.Future; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | +import org.apache.log4j.Logger; |
| 13 | + |
| 14 | +/** |
| 15 | + * Future、Callable练习。 |
| 16 | + * |
| 17 | + * @author <a href="mailto:[email protected]">聂勇</a> |
| 18 | + */ |
| 19 | +public class HelloFuture { |
| 20 | + |
| 21 | + private static Logger _logger = Logger.getLogger(HelloFuture.class); |
| 22 | + |
| 23 | + /** |
| 24 | + * @param args |
| 25 | + * @throws InterruptedException |
| 26 | + */ |
| 27 | + public static void main(String[] args) throws InterruptedException { |
| 28 | + executeSingleTask(); |
| 29 | + executeBatchTask(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * 执行单个异步任务:<br> |
| 34 | + * 1、用submit方法向线程池提交一个任务。<br> |
| 35 | + * 2、获取结果时,指定了超时时间。<br><br> |
| 36 | + * 结果:超时后,调用者收到TimeoutException,但实际上任务还在继续执行。 |
| 37 | + * @throws InterruptedException |
| 38 | + */ |
| 39 | + private static void executeSingleTask() throws InterruptedException { |
| 40 | + ExecutorService threadpool = Executors.newFixedThreadPool(6); |
| 41 | + try { |
| 42 | + Future<Integer> f = threadpool.submit(createCallable(5 * 1000)); |
| 43 | + Object result = f.get(3, TimeUnit.SECONDS); |
| 44 | + System.out.println("单个任务的执行结果:"+result); |
| 45 | + } catch (Exception e) { |
| 46 | + _logger.error("线程执行任务时出错", e); |
| 47 | + } finally { |
| 48 | + threadpool.shutdown(); |
| 49 | + threadpool.awaitTermination(10, TimeUnit.SECONDS); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * 执行多个异步任务:<br> |
| 55 | + * 1、用invokeAll方法向线程池提交多个任务,并指定了执行的超时时间。<br><br> |
| 56 | + * 结果:超时后,未执行完成的任务被取消,在调用Future的get方法时,取消的任务会抛出CancellationException,执行完成的任务可获得结果。 |
| 57 | + * @throws InterruptedException |
| 58 | + */ |
| 59 | + private static void executeBatchTask() throws InterruptedException { |
| 60 | + ExecutorService threadpool = Executors.newFixedThreadPool(6); |
| 61 | + List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); |
| 62 | + tasks.add(createCallable(2000)); |
| 63 | + tasks.add(createCallable(5000)); |
| 64 | + tasks.add(createCallable(2500)); |
| 65 | + |
| 66 | + long startTime = System.currentTimeMillis(); |
| 67 | + try { |
| 68 | + List<Future<Integer>> fs = threadpool.invokeAll(tasks, 3, TimeUnit.SECONDS); |
| 69 | + int result = 0; |
| 70 | + for (Future<Integer> f : fs) { |
| 71 | + try{ |
| 72 | + result += f.get(); |
| 73 | + } catch(CancellationException ce) { |
| 74 | + // nothing |
| 75 | + } |
| 76 | + } |
| 77 | + System.out.println("执行三个任务共耗时:" + (System.currentTimeMillis() - startTime) + "毫秒"); |
| 78 | + System.out.println("三个任务的执行结果汇总:"+result); |
| 79 | + } catch (Exception e) { |
| 80 | + _logger.error("线程执行任务时出错", e); |
| 81 | + } finally { |
| 82 | + threadpool.shutdown(); |
| 83 | + threadpool.awaitTermination(10, TimeUnit.SECONDS); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 创建需要长时间执行的任务模拟对象。 |
| 89 | + * @param sleepTimes 线程休眠时间(单位:毫秒) |
| 90 | + * @return {@link Callable}对象 |
| 91 | + */ |
| 92 | + private static Callable<Integer> createCallable(final int sleepTimes) { |
| 93 | + Callable<Integer> c = new Callable<Integer>() { |
| 94 | + |
| 95 | + @Override |
| 96 | + public Integer call() throws Exception { |
| 97 | + Thread.sleep(sleepTimes); |
| 98 | + System.out.println(Thread.currentThread().getName() + ": I'm working"); |
| 99 | + return 9; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + return c; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments