jdk8之前的实现方式,在JUC下增加了Future,从字面意思理解就是未来的意思,但使用起来却着实有点鸡肋,并不能实现真正意义上的异步,获取结果时需要阻塞线程,或者不断轮询。
@Test publicvoidtest1()throwsException{ System.out.println("main函数开始执行"); ExecutorServiceexecutor=Executors.newFixedThreadPool(1); Future<Integer>future=executor.submit(newCallable<Integer>(){ @Override publicIntegercall()throwsException{ System.out.println("===taskstart==="); Thread.sleep(5000); System.out.println("===taskfinish==="); return3; } }); //这里需要返回值时会阻塞主线程,如果不需要返回值使用是OK的。倒也还能接收 //Integerresult=future.get(); System.out.println("main函数执行结束"); System.in.read(); }使用原生的CompletableFuture实现异步操作,加上对lambda的支持,可以说实现异步任务已经发挥到了极致。
@Test publicvoidtest2()throwsException{ System.out.println("main函数开始执行"); ExecutorServiceexecutor=Executors.newFixedThreadPool(2); CompletableFuture<Integer>future=CompletableFuture.supplyAsync(newSupplier<Integer>(){ @Override publicIntegerget(){ System.out.println("===taskstart==="); try{ Thread.sleep(5000); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println("===taskfinish==="); return3; } },executor); future.thenAccept(e->System.out.println(e)); System.out.println("main函数执行结束"); }使用spring实现异步需要开启注解,可以使用xml方式或者javaconfig的方式。
xml方式:<task:annotation-driven/>
<task:annotation-drivenexecutor="executor"/> <task:executorid="executor" pool-size="2"线程池的大小 queue-capacity="100"排队队列长度 keep-alive="120"线程保活时间(单位秒) rejection-policy="CALLER_RUNS"对拒绝的任务处理策略/>java方式:
@EnableAsync publicclassMyConfig{ @Bean publicTaskExecutorexecutor(){ ThreadPoolTaskExecutorexecutor=newThreadPoolTaskExecutor(); executor.setCorePoolSize(10);//核心线程数 executor.setMaxPoolSize(20);//最大线程数 executor.setQueueCapacity(1000);//队列大小 executor.setKeepAliveSeconds(300);//线程最大空闲时间 executor.setThreadNamePrefix("fsx-Executor-");//指定用于新创建的线程名称的前缀。 executor.setRejectedExecutionHandler(newThreadPoolExecutor.CallerRunsPolicy()); returnexecutor; } } (1)@Async @Test publicvoidtest3()throwsException{ System.out.println("main函数开始执行"); myService.longtime(); System.out.println("main函数执行结束"); } @Async publicvoidlongtime(){ System.out.println("我在执行一项耗时任务"); try{ Thread.sleep(5000); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println("完成"); } (2)AsyncResult如果需要返回值,耗时方法返回值用AsyncResult包装。
@Test publicvoidtest4()throwsException{ System.out.println("main函数开始执行"); Future<Integer>future=myService.longtime2(); System.out.println("main函数执行结束"); System.out.println("异步执行结果:"+future.get()); } @Async publicFuture<Integer>longtime2(){ System.out.println("我在执行一项耗时任务"); try{ Thread.sleep(8000); }catch(InterruptedExceptione){ e.printStackTrace(); } System.out.println("完成"); returnnewAsyncResult<>(3); }本文内容总结:一、创建线程,二、Future,三、CompletableFuture,四、Spring的Async注解,,
原文链接:https://www.cnblogs.com/sword-successful/p/11181714.html