SpringBootラーニング--非同期インタフェースの構成と呼び出し
34151 ワード
SpringBootラーニング–非同期インタフェースの構成と呼び出し
概要
SpringBoot環境の構築
mavenプロジェクトを構築します.pom.xmlファイル導入依存
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
構成クラスAsyncConfig.java
package com.pzr.demo.config;
import java.util.concurrent.Executor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
*
*
* @author ASUS
*
*/
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
//
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);// ,
taskExecutor.setMaxPoolSize(10);//
taskExecutor.setQueueCapacity(25);//
taskExecutor.setKeepAliveSeconds(30000);//
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
制御層クラスAsyncController.java
2つの例(1:すべてのメソッド非同期:.2:まず非同期、後集団待機、最後に次の非同期操作を実行)を含む.
package com.pzr.demo.controller;
import java.util.Date;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pzr.demo.service.AsyncService;
/**
*
* @author ASUS
*
*/
@Controller
@RequestMapping("asyncController")
public class AsyncController {
@Autowired
AsyncService asyncService;
/**
*
* @return
*/
@RequestMapping("asyncTest")
@ResponseBody
public String asyncTest(){
Date start = new Date();
asyncService.task1();
asyncService.task2();
asyncService.task3();
String resultStr = " :"+((new Date()).getTime() - start.getTime());
System.out.println(resultStr);
return resultStr;
}
/**
* ,
* @return
*/
@RequestMapping("cyclicBarrierTest")
@ResponseBody
public String cyclicBarrierTest(){
Date start = new Date();
Future<String> task1 = asyncService.task1();
Future<String> task2 = asyncService.task2();
Future<String> task3 = asyncService.task3();
while(true){
if(task1.isDone() && task2.isDone() && task3.isDone()){
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String resultStr = " :"+((new Date()).getTime() - start.getTime());
System.out.println(resultStr);
return resultStr;
}
}
ビジネス・レベル・インタフェースjava
package com.pzr.demo.service;
import java.util.concurrent.Future;
/**
*
* @author ASUS
*
*/
public interface AsyncService {
public Future<String> task1();
public Future<String> task2();
public Future<String> task3();
}
ビジネス層実装クラスAsyncServiceImpl.java
package com.pzr.demo.service.impl;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import com.pzr.demo.service.AsyncService;
@Service
public class AsyncServiceImpl implements AsyncService {
@Override
@Async
public Future<String> task1() {
Future<String> result =null;
try {
Thread.sleep(10000);
System.out.println(" 1: 10 "+Thread.currentThread().getName());
result =new AsyncResult<String>(" 1 ");
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
@Override
@Async
public Future<String> task2() {
Future<String> result =null;
try {
Thread.sleep(5000);
System.out.println(" 2: 5 "+Thread.currentThread().getName());
result =new AsyncResult<String>(" 2 ");
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
@Override
@Async
public Future<String> task3() {
Future<String> result =null;
try {
Thread.sleep(15000);
System.out.println(" 3: 15 "+Thread.currentThread().getName());
result =new AsyncResult<String>(" 3 ");
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
結果
すべての非同期は,プライマリスレッドがサブスレッドの影響を受けないことを示し,最初に完了してデータを返し,サブスレッドは睡眠時間の長さに応じて順次印刷される.
:4
2: 5 ThreadPoolTaskExecutor-2
1: 10 ThreadPoolTaskExecutor-1
3: 15 ThreadPoolTaskExecutor-3
まず非同期で、後で集団で実行を待つ次のステップは、タスク1,2,3が非同期で実行されていることを示し、メインスレッドはサブスレッドが完了してから印刷を行い、総消費時間は最も長いサブスレッド時間に近い.
2: 5 ThreadPoolTaskExecutor-5
1: 10 ThreadPoolTaskExecutor-4
3: 15 ThreadPoolTaskExecutor-2
:15008
リファレンス
https://blog.csdn.net/qq_41396619/article/details/81005084 https://blog.csdn.net/v2sking/article/details/72795742
プロジェクトGITアドレス
https://gitee.com/flygoa/springbootdemo1.git