머리말
이전 Spring Boot Basic Tutorial 시리즈에서 "Spring Boot에서 비동기 통화를 구현하기 위해 @async를 사용하여"@async 주석을 사용하여 비동기 통화를 구현하는 방법이 소개되었습니다. 그러나 이러한 비동기 실행에 대한 제어는 자체 응용 프로그램의 건강을 보장하는 기본 기술입니다. 이 기사에서는 사용자 정의 스레드 풀을 통해 비동기 통화 동시성을 제어하는지 알아 보겠습니다.
이전 예제를 기반 으로이 기사의 예제를 수정하거나 시도 할 새로운 스프링 부팅 프로젝트를 만들 수 있습니다.
스레드 풀을 정의하십시오
첫 번째 단계는 Spring Boot 메인 클래스에서 스레드 풀을 정의하는 것입니다.
@SpringBootApplicationPublic Class Application {public static void main (String [] args) {springApplication.run (application.class, args); } @enableAsync @configuration class taskpoolconfig {@bean ( "taskexecutor") 공개 집행자 taskexecutor () {ThreadPooltasKexEcutor Executor = new ThreadPoolTasKexEcutor (); executor.setCorePoolSize (10); executor.setmaxpoolsize (20); executor.setqueuecapacity (200); executor.setKeepaliveseconds (60); executor.setThreadnamEprefix ( "taskexecutor-"); executor.setRejectedExecutionHandler (new ThreadPooleExecutor.callerRunspolicy ()); 귀환 집행자; }}}위에서 우리는 ThreadPooltasKexEcutor를 사용하여 스레드 풀을 만들고 다음 매개 변수를 설정했습니다.
스레드 풀 사용
스레드 풀을 정의한 후이 스레드 풀의 리소스를 사용하여 비동기 적으로 실행 작업을 실행할 수 있습니까? 이 방법은 매우 간단합니다. 예를 들어 @async 주석에 스레드 풀 이름을 지정하면됩니다.
@slf4j@componentpublic class task {public static random = new random (); @async ( "taskexecutor") public void dotaskone ()는 예외 {log.info ( "start task one"); Long Start = System.CurrentTimeMillis (); thread.sleep (random.nextint (10000)); Long End = System.CurrentTimeMillis (); log.info ( "완전한 작업 1, 시간 :" + (종료 - 시작) + "milliseconds"); } @async ( "taskexecutor") public void dotasktwo ()는 예외 {log.info ( "start task 2"); Long Start = System.CurrentTimeMillis (); thread.sleep (random.nextint (10000)); Long End = System.CurrentTimeMillis (); log.info ( "완전한 작업 2, 시간을 찍는 시간 :" + (END -Start) + "milliseconds"); } @async ( "taskexecutor") public void dotaskthree ()는 예외 {log.info ( "시작 작업 3"); Long Start = System.CurrentTimeMillis (); thread.sleep (random.nextint (10000)); Long End = System.CurrentTimeMillis (); log.info ( "완전한 작업 3, 시간 소모 :" + (END -Start) + "milliseconds"); }}단위 테스트
마지막으로, 단위 테스트를 작성하여 확인해 봅시다
@RunWith (SpringJunit4classRunner.class) @SpringBootTestPublic Class ApplicationTests {@autowired 개인 작업 작업; @test public void test ()는 예외 {task.dotaskone (); task.dotasktwo (); task.dotaskthree (); thread.currentthread (). join (); }}위의 단위 테스트를 수행하십시오. 콘솔에서 모든 출력 스레드 이름 이름이 이전에 정의한 스레드 풀에 의해 접두사가 표시되는 것을 볼 수 있습니다. 즉, 스레드 풀을 사용하여 비동기 작업을 수행하는 실험이 성공적이었습니다!
2018-03-27 22 : 01 : 15.620 정보 73703 --- [taskexecutor-1] com.didispace.async.task : 작업 1을 시작하십시오.
2018-03-27 22 : 01 : 15.620 정보 73703 --- [taskexecutor-2] com.didispace.async.task : 작업 2 시작 2
2018-03-27 22 : 01 : 15.620 정보 73703 --- [taskexecutor-3] com.didispace.async.task : 작업 3 시작
2018-03-27 22 : 01 : 18.165 정보 73703 --- [taskexecutor-2] com.didispace.async.task : 완료 작업 2, 시간 : 2545 밀리 초
2018-03-27 22 : 01 : 22.149 정보 73703 --- [taskexecutor-3] com.didispace.async.task : 완료 작업 3, 시간 : 6529 밀리 초
2018-03-27 22 : 01 : 23.912 정보 73703 --- [taskexecutor-1] com.didispace.async.task : 완료 작업 1, 시간 : 8292 밀리 초
Complete example:
독자는 자신의 선호에 따라 4-1-3 장 프로젝트를 볼 수 있습니다.
github : https://github.com/dyc87112/springboot-learning/
Gitee : https://gitee.com/diidispace/springboot-learning/
로컬 다운로드 : http://xiazai.vevb.com/201805/yuanma/springboot-learning(Vevb.com).rar
요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.