日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
怎么在SpringBoot中異步調(diào)用方法并接收返回值

怎么在SpringBoot中異步調(diào)用方法并接收返回值?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站是專業(yè)的西夏網(wǎng)站建設(shè)公司,西夏接單;提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行西夏網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!

步驟1:配置線程池,添加@Configuration和@EnableAsync注解

@Configuration
@EnableAsync
public class ExecutorConfig {
 
 
  /**
   * 線程池
   *
   * @return
   */
  @Bean(name = "asyncExecutor")
  public Executor asyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(10);
    executor.setMaxPoolSize(15);
    executor.setQueueCapacity(25);
    executor.setKeepAliveSeconds(200);
    executor.setThreadNamePrefix("async-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    // 等待所有任務(wù)都完成再繼續(xù)銷毀其他的Bean
    executor.setWaitForTasksToCompleteOnShutdown(true);
    // 線程池中任務(wù)的等待時間,如果超過這個時候還沒有銷毀就強制銷毀,以確保應(yīng)用最后能夠被關(guān)閉,而不是阻塞住
    executor.setAwaitTerminationSeconds(60);
    executor.initialize();
    return executor;
  }
 
 
}

步驟2:定義方法A,方法B,方法C,方法D

@Service
public class AsyncService {
 
  @Async("asyncExecutor")
  public Future methodB(){
    try{
      Thread.sleep(1000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new AsyncResult<>(1);
  }
 
  @Async("asyncExecutor")
  public Future methodC(){
    try{
      Thread.sleep(2000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new AsyncResult<>(2);
  }
 
  @Async("asyncExecutor")
  public Future methodD(){
    try{
      Thread.sleep(3000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new AsyncResult<>(3);
  }
}
@GetMapping("test")
  public Integer methodA() throws Exception{
    long start = System.currentTimeMillis();
    Future future1 = asyncService.methodB();
    Future future2 = asyncService.methodC();
    Future future3 = asyncService.methodD();
    Integer x = future1.get();
    Integer y = future2.get();
    Integer z = future3.get();
    long end = System.currentTimeMillis();
    System.out.println("耗時:" + (end - start));
    return x + y +z;
  }
}

結(jié)果:

怎么在SpringBoot中異步調(diào)用方法并接收返回值

怎么在SpringBoot中異步調(diào)用方法并接收返回值

springboot是什么

springboot一種全新的編程規(guī)范,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。


標(biāo)題名稱:怎么在SpringBoot中異步調(diào)用方法并接收返回值
本文鏈接:http://www.dlmjj.cn/article/ghpoid.html