新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
實(shí)用java實(shí)現(xiàn)異步調(diào)用
本篇文章為大家分享一下java實(shí)現(xiàn)異步調(diào)用的具體方法,有需要的小伙伴可以參考一下。

一、創(chuàng)建線程
@Test
public void test0() throws Exception {
System.out.println("main函數(shù)開始執(zhí)行");
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("===task start===");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("===task finish===");
}
});
thread.start();
System.out.println("main函數(shù)執(zhí)行結(jié)束");
}
二、Future
jdk8之前的實(shí)現(xiàn)方式,在JUC下增加了Future,從字面意思理解就是未來的意思,但使用起來卻著實(shí)有點(diǎn)雞肋,并不能實(shí)現(xiàn)真正意義上的異步,獲取結(jié)果時(shí)需要阻塞線程,或者不斷輪詢。
@Test
public void test1() throws Exception {
System.out.println("main函數(shù)開始執(zhí)行");
ExecutorService executor = Executors.newFixedThreadPool(1);
Future
future = executor.submit(new Callable
() { @Override public Integer call() throws Exception { System.out.println(
"===task start==="); Thread.sleep(5000); System.out.println(
"===task finish===");
return 3; } }); //這里需要返回值時(shí)會(huì)阻塞主線程,如果不需要返回值使用是OK的。倒也還能接收 //Integer result=future.get(); System.out.println(
"main函數(shù)執(zhí)行結(jié)束"); System.in.read(); }
三、CompletableFuture
使用原生的CompletableFuture實(shí)現(xiàn)異步操作,加上對lambda的支持,可以說實(shí)現(xiàn)異步任務(wù)已經(jīng)發(fā)揮到了極致。
@Test
public void test2() throws Exception {
System.out.println("main函數(shù)開始執(zhí)行");
ExecutorService executor = Executors.newFixedThreadPool(2);
CompletableFuture
future = CompletableFuture.supplyAsync(new Supplier
() { @Override public Integer
get() { System.out.println(
"===task start==="); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(
"===task finish===");
return 3; } }, executor); future.thenAccept(e -> System.out.println(e)); System.out.println(
"main函數(shù)執(zhí)行結(jié)束"); }
四、Spring的Async注解
使用spring實(shí)現(xiàn)異步需要開啟注解,可以使用xml方式或者Java config的方式。
xml方式:
"executor" />
"executor" pool-size=
"2" 線程池的大小 queue-capacity=
"100" 排隊(duì)隊(duì)列長度 keep-alive=
"120" 線程?;顣r(shí)間(單位秒) rejection-policy=
"CALLER_RUNS" 對拒絕的任務(wù)處理策略 />
java方式:
@EnableAsync
public class MyConfig {
@Bean
public TaskExecutor executor(){
ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10); //核心線程數(shù)
executor.setMaxPoolSize(20); //最大線程數(shù)
executor.setQueueCapacity(1000); //隊(duì)列大小
executor.setKeepAliveSeconds(300); //線程最大空閑時(shí)間
executor.setThreadNamePrefix("fsx-Executor-"); //指定用于新創(chuàng)建的線程名稱的前綴。
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
(1)@Async
@Test
public void test3() throws Exception {
System.out.println("main函數(shù)開始執(zhí)行");
myService.longtime();
System.out.println("main函數(shù)執(zhí)行結(jié)束");
}
@Async
public void longtime() {
System.out.println("我在執(zhí)行一項(xiàng)耗時(shí)任務(wù)");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("完成");
}
(2)AsyncResult
如果需要返回值,耗時(shí)方法返回值用AsyncResult包裝。
@Test
public void test4() throws Exception {
System.out.println("main函數(shù)開始執(zhí)行");
Future future=myService.longtime2();
System.out.println("main函數(shù)執(zhí)行結(jié)束");
System.out.println("異步執(zhí)行結(jié)果:"+future.get());
}
@Async
public Future longtime2() {
System.out.println("我在執(zhí)行一項(xiàng)耗時(shí)任務(wù)");
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("完成");
return new AsyncResult(3);
}
網(wǎng)頁題目:實(shí)用java實(shí)現(xiàn)異步調(diào)用
標(biāo)題路徑:http://www.dlmjj.cn/article/dppcicj.html


咨詢
建站咨詢
