91在线一级黄片|91视频在线观看18|成人夜间呦呦网站|91资源欧美日韩超碰|久久最新免费精品视频一区二区三区|国产探花视频在线观看|黄片真人免费三级片毛片|国产人无码视频在线|精品成人影视无码三区|久久视频爱久久免费精品

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

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
實用java實現(xiàn)異步調用

本篇文章為大家分享一下java實現(xiàn)異步調用的具體方法,有需要的小伙伴可以參考一下。

一、創(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í)行結束");

}

二、Future

jdk8之前的實現(xiàn)方式,在JUC下增加了Future,從字面意思理解就是未來的意思,但使用起來卻著實有點雞肋,并不能實現(xiàn)真正意義上的異步,獲取結果時需要阻塞線程,或者不斷輪詢。

@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;        }    });    //這里需要返回值時會阻塞主線程,如果不需要返回值使用是OK的。倒也還能接收    //Integer result=future.get();    System.out.println(
    "main函數(shù)執(zhí)行結束");    System.in.read(); } 
   

三、CompletableFuture

使用原生的CompletableFuture實現(xiàn)異步操作,加上對lambda的支持,可以說實現(xiàn)異步任務已經發(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í)行結束"); } 
   

四、Spring的Async注解

使用spring實現(xiàn)異步需要開啟注解,可以使用xml方式或者Java config的方式。

xml方式:


  
   "executor" /> 
   
    "executor"        pool-size=
    "2" 線程池的大小        queue-capacity=
    "100" 排隊隊列長度        keep-alive=
    "120" 線程?;顣r間(單位秒)        rejection-policy=
    "CALLER_RUNS" 對拒絕的任務處理策略 /> 
   

java方式:

@EnableAsync
public class MyConfig {

   @Bean
   public TaskExecutor executor(){
       ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();
       executor.setCorePoolSize(10); //核心線程數(shù)
       executor.setMaxPoolSize(20);  //最大線程數(shù)
       executor.setQueueCapacity(1000); //隊列大小
       executor.setKeepAliveSeconds(300); //線程最大空閑時間
       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í)行結束");
}

@Async
public void longtime() {
   System.out.println("我在執(zhí)行一項耗時任務");
   try {
       Thread.sleep(5000);
   } catch (InterruptedException e) {
       e.printStackTrace();
   }
   System.out.println("完成");

}

(2)AsyncResult

如果需要返回值,耗時方法返回值用AsyncResult包裝。

@Test
public void test4() throws Exception {
   System.out.println("main函數(shù)開始執(zhí)行");
   Future future=myService.longtime2();
   System.out.println("main函數(shù)執(zhí)行結束");
   System.out.println("異步執(zhí)行結果:"+future.get());
}

@Async
public Future longtime2() {
   System.out.println("我在執(zhí)行一項耗時任務");

   try {
       Thread.sleep(8000);
   } catch (InterruptedException e) {
       e.printStackTrace();
   }

   System.out.println("完成");
   return new AsyncResult(3);
}

網站標題:實用java實現(xiàn)異步調用
網站URL:http://m.jiaoqi3.com/article/dppcicj.html