日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
詳解Java異步調(diào)用

在長(zhǎng)期的Java客戶端開(kāi)發(fā)中,最常見(jiàn)的一個(gè)客戶端調(diào)用模式就是Java的異步調(diào)用。所謂異步調(diào)用其實(shí)就是實(shí)現(xiàn)一個(gè)可無(wú)需等待被調(diào)用函數(shù)的返回值而讓操作繼續(xù)運(yùn)行的方法。在Java語(yǔ)言中,簡(jiǎn)單的講就是另啟一個(gè)線程來(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)電咨詢建站服務(wù):028-86922220

一、創(chuàng)建線程

@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í)需要阻塞線程,或者不斷輪詢。

@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ì)阻塞主線程,如果不需要返回值使用是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" 線程池的大小        queue-capacity=
    "100" 排隊(duì)隊(duì)列長(zhǎng)度        keep-alive=
    "120" 線程保活時(shí)間(單位秒)        rejection-policy=
    "CALLER_RUNS" 對(duì)拒絕的任務(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)建的線程名稱(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);
}

分享名稱(chēng):詳解Java異步調(diào)用
本文路徑:http://www.5511xx.com/article/djpesij.html