新聞中心
在長(zhǎng)期的Java客戶(hù)端開(kāi)發(fā)中,最常見(jiàn)的一個(gè)客戶(hù)端調(diào)用模式就是Java的異步調(diào)用。所謂異步調(diào)用其實(shí)就是實(shí)現(xiàn)一個(gè)可無(wú)需等待被調(diào)用函數(shù)的返回值而讓操作繼續(xù)運(yùn)行的方法。在Java語(yǔ)言中,簡(jiǎn)單的講就是另啟一個(gè)線(xiàn)程來(lái)完成調(diào)用中的部分計(jì)算,使調(diào)用繼續(xù)運(yùn)行或返回,而不需要等待計(jì)算結(jié)果。

創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)尉氏,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):028-86922220
一、創(chuàng)建線(xiàn)程
@Test
public void test0() throws Exception {
System.out.println("main函數(shù)開(kāi)始執(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,從字面意思理解就是未來(lái)的意思,但使用起來(lái)卻著實(shí)有點(diǎn)雞肋,并不能實(shí)現(xiàn)真正意義上的異步,獲取結(jié)果時(shí)需要阻塞線(xiàn)程,或者不斷輪詢(xún)。
@Test
public void test1() throws Exception {
System.out.println("main函數(shù)開(kāi)始執(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ì)阻塞主線(xiàn)程,如果不需要返回值使用是OK的。倒也還能接收 //Integer result=future.get(); System.out.println(
"main函數(shù)執(zhí)行結(jié)束"); System.in.read(); }
三、CompletableFuture
使用原生的CompletableFuture實(shí)現(xiàn)異步操作,加上對(duì)lambda的支持,可以說(shuō)實(shí)現(xiàn)異步任務(wù)已經(jīng)發(fā)揮到了極致。
@Test
public void test2() throws Exception {
System.out.println("main函數(shù)開(kāi)始執(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)異步需要開(kāi)啟注解,可以使用xml方式或者Java config的方式。
xml方式:
"executor" />
"executor" pool-size=
"2" 線(xiàn)程池的大小 queue-capacity=
"100" 排隊(duì)隊(duì)列長(zhǎng)度 keep-alive=
"120" 線(xiàn)程?;顣r(shí)間(單位秒) rejection-policy=
"CALLER_RUNS" 對(duì)拒絕的任務(wù)處理策略 />
java方式:
@EnableAsync
public class MyConfig {
@Bean
public TaskExecutor executor(){
ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10); //核心線(xiàn)程數(shù)
executor.setMaxPoolSize(20); //最大線(xiàn)程數(shù)
executor.setQueueCapacity(1000); //隊(duì)列大小
executor.setKeepAliveSeconds(300); //線(xiàn)程最大空閑時(shí)間
executor.setThreadNamePrefix("fsx-Executor-"); //指定用于新創(chuàng)建的線(xiàn)程名稱(chēng)的前綴。
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
(1)@Async
@Test
public void test3() throws Exception {
System.out.println("main函數(shù)開(kāi)始執(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ù)開(kāi)始執(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);
}
文章題目:詳解Java異步調(diào)用
地址分享:http://m.fisionsoft.com.cn/article/djpesij.html


咨詢(xún)
建站咨詢(xún)
