작업 및 멀티 스레드 구성 타이밍 달성 @Scheduled 봄 부팅 배

@Scheduled 쉽게 일반 작업을 사용하여 달성 될 수있다

2.1.6.RELEASE의 봄 부팅 버전

패키지 com.abc.demo.common; 

수입 org.slf4j.Logger;
수입 org.slf4j.LoggerFactory;
수입 org.springframework.scheduling.annotation.EnableScheduling;
가져 오기 org.springframework.scheduling.annotation.Scheduled;
수입 org.springframework.stereotype.Component; 

수입 java.text.SimpleDateFormat에;
수입 java.util.concurrent.TimeUnit을; 

@EnableScheduling 
@Component 
공공  클래스 ScheduleSetting을 { 

    민간  최종 로거 로거 = LoggerFactory.getLogger (. 작업 클래스 );

    @Scheduled (있도록, fixedRate= 10000 = 2000 initialDelay의 )
     공공  무효 scheduleRead는 () {
         시도 {
             오래 때문에, timeStamp = 에 System.currentTimeMillis (); 
            SimpleDateFormat의 SimpleDateFormat는 = 새로운 SimpleDateFormat의 ( "YYYY-MM-DD HH : MM : SS" ); 
            스레드 스레드 = 는 Thread.currentThread (); 
            에서 System.out.println ( "cron1任务开始의 시작 ="+ simpleDateFormat.format (때문에, timeStamp) + "threadId ="+ thread.getId () + "threadName ="+ thread.getName ());
             endStamp = System.currentTimeMillis는 ();
            시도 {
                TimeUnit.SECONDS.sleep ( 20 인 ); 
            } 캐치 (예외 : InterruptedException E) { 
                ; e.printStackTrace () 
            } 
            에서 System.out.println ( + thread.getName () + "끝 시작"작업을 실행 이름 cron1 스레드 " = "+ simpleDateFormat.format (때문에, timeStamp) +", 종료 = "+ simpleDateFormat.format (endStamp)) 
            에서 System.out.println ( "+++++++++++++++++++ +++++ ' ); 
        } 캐치 (예외 E) { 
            logger.error (e.getMessage ()); 
        } 
    } 

    (있도록, fixedRate @Scheduled = 5000initialDelay의 = 1000 )
     공개  무효scheduleConvert는 () {
         시도 { 

            오래 때문에, timeStamp = 에 System.currentTimeMillis (); 
            SimpleDateFormat의 SimpleDateFormat는 = 새로운 SimpleDateFormat의 ( "YYYY-MM-DD HH : MM : SS" ); 
            스레드 스레드 = 는 Thread.currentThread (); 
            에서 System.out.println ( "CRON2任务开始의 시작 ="+ simpleDateFormat.format (때문에, timeStamp) + "threadId ="+ thread.getId () + "threadName ="+ thread.getName ());
            시도 { 
                TimeUnit.SECONDS.sleep을 ( 10 ); 
            } 캐치 (예외 : InterruptedException 전자) { 
                ) (e.printStackTrace을; 
            }
             endStamp = 에 System.currentTimeMillis (); 
            에서 System.out.println ( "작업 실행 이름 CRON2 스레드 :"+ thread.getName () + + simpleDateFormat.format (timestamp "를 끝 = 시작" "+), 끝 = "+ simpleDateFormat.format (endStamp)) 
            에서 System.out.println ( "==================== ' ); 
        } 캐치 (예외 E) { 
            logger.error (e.getMessage ()); 
        } 
    } 

}

출력 내용이 실행

CRON2 작업 = 2019년 10월 11일 17시 31분 52초, threadId = 시작 시작 (34), threadName = 일정-1
작업 실행 스레드 이름 CRON2을 : 일정-1 끝 = 2019년 10월 11일 17시 31분를 시작합니다 : 52, 끝 = 2019년 10월 11일 17시 32분 2초
====================
cron1 작업이 시작, 시작 = 2019년 10월 11일 17시 32분 2초 , threadId = 34 = threadName 스케줄링 -1
스레드 이름 cron1 실행 태스크 스케쥴링 1 단부를 시작 2019년 10월 11일 = 17 : 32 : 02 = 단부 2019년 10월 11일 17시 32분 2초
++ ++++++++++++++++++++++
CRON2 작업이 시작 = 2019년 10월 11일 17시 32분 22초의 threadId = 34이며, threadName = 일정. 1 시작
CRON2 작업을 예약 1 단부 시작 2019년 10월 11일 = 17 : 32 : 실행 스레드 이름 (22), 단부 = 2019년 10월 11일 17시 32분 32초을

......

참고 :

  이행 후의 CRON2는 cron1 수행 될

이유 :

  봄의 기본값은 단일 스레드 실행 작업 일정입니다

  스레드의 봄 타이밍 작업의 기본 최대 수는 실행 1에 문제가있을 것입니다 여러 작업 시간을 실행

1. 구성 스레드 풀

application.properties의 구성 파일에 추가

# 스레드 풀 크기 
spring.task.scheduling.pool.size = 5 
# 스레드 이름 접두사 
spring.task.scheduling.thread의 -name - 접두사 = myScheduling-

출력 내용이된다

CRON2 작업 (34)가 threadId = = myScheduling - threadName이다, 시작 = 2019년 10월 11일 17시 34분 48초 시작합니다. 1
cron1 작업을 시작, 시작 = 2019년 10월 11일 17시 34분 49초, threadId = 35, threadName = myScheduling -2
스레드가 작업 이름 CRON2 실행 : myScheduling-1 엔드를 시작 = 2019년 10월 11일 17 :. (34)은 : 48, 끝 = 2019년 10월 11일 17시 34분 58초
========== ==========
CRON2 작업이 시작 = 2019년 10월 11일 17시 34분 58초, threadId = 시작 (34), threadName = myScheduling-1
myScheduling 1 끝, 시작 : 스레드 이름이 작업을 실행 CRON2을 2019년 10월 11일 (17) =. 34 인 : 58, 종료 = 2019년 10월 11일 17시 35분 8초
====================
CRON2 작업이 시작, 시작 = 2019년 10월 11일 17시 35분 8초, threadId = 57 threadName = myScheduling -3-
작업 이름 cron1 실행 스레드 : myScheduling-2 단부를 시작 2019년 10월 11일 = 17 : 34 : 49, 단부 = 2019- 10 ~ 11 17시 34분 49초

......

참고 :

  다중 스레드, cron1 및 CRON2 서로를 기다릴 필요가 있지만, 동일한 작업이나 기다려야하지 않는다

2. 동시

코드를 수정

패키지 com.abc.demo.common; 

수입 org.slf4j.Logger;
수입 org.slf4j.LoggerFactory;
수입 org.springframework.scheduling.annotation.Async;
수입 org.springframework.scheduling.annotation.EnableAsync;
수입 org.springframework.scheduling.annotation.EnableScheduling;
가져 오기 org.springframework.scheduling.annotation.Scheduled;
수입 org.springframework.stereotype.Component; 

수입 java.text.SimpleDateFormat에;
수입 java.util.concurrent.TimeUnit을; 

@EnableScheduling 
@Component 
@EnableAsync 
공공  클래스 ScheduleSetting {

    민간  최종 로거 로거 = LoggerFactory.getLogger (작업. 클래스 );

    @Async 
    @Scheduled (있도록, fixedRate = 10000, initialDelay의 = 2000 )
     공공  무효 scheduleRead는 () {
         시도 {
             오래 때문에, timeStamp = 에 System.currentTimeMillis (); 
            SimpleDateFormat의 SimpleDateFormat는 = 새로운 SimpleDateFormat의 ( "YYYY-MM-DD HH : MM : SS" ); 
            스레드 스레드 = 는 Thread.currentThread (); 
            에서 System.out.println ("threadName ="+ + "threadId ="+ thread.getId () + + simpleDateFormat.format (때문에, timeStamp) "cron1任务의开始= 시작" ) thread.getName ();
             endStamp = System.currentTimeMillis는 ();
            시도 { 
                TimeUnit.SECONDS.sleep을 ( 20 ); 
            } 캐치 (예외 : InterruptedException 전자) { 
                e.printStackTrace (); 
            } 
            에서 System.out.println ( "cron1任务正在运行的线程名称"+ thread.getName () + "结束이 시작 ="+ simpleDateFormat.format (때문에, timeStamp) + "단부 ="+ simpleDateFormat.format (endStamp) ); 
            에서 System.out.println ( "
        } 캐치 (예외 전자) { 
            logger.error (e.getMessage ()); 
        } 
    } 

    @Async 
    @Scheduled (있도록, fixedRate = 5000 = 1000가 initialDelay가 )
     공공  공극 scheduleConvert는 () {
         시도 { 

             때문에, timeStamp = System.currentTimeMillis는 (); 
            SimpleDateFormat의 SimpleDateFormat는 = 새로운 SimpleDateFormat의 ( "YYYY-MM-DD HH : MM : SS" ); 
            스레드 스레드 = 는 Thread.currentThread (); 
            에서 System.out.println ("threadName ="+ + "threadId ="+ thread.getId () + + simpleDateFormat.format (때문에, timeStamp) "CRON2任务의开始= 시작" ) thread.getName ();
            시도 { 
                TimeUnit.SECONDS.sleep을 ( 10 ); 
            } 캐치 (예외 : InterruptedException 전자) { 
                e.printStackTrace (); 
            } 
             endStamp = System.currentTimeMillis는 (); 
            에서 System.out.println ( "CRON2任务正在运行的线程名称"+ thread.getName () + "结束이 시작 ="+ simpleDateFormat.format (때문에, timeStamp) + "단부 ="+ simpleDateFormat.format (endStamp)) ; 
            에서 System.out.println ( "==============" ); 
        }캐치 (예외 전자) { 
            logger.error (e.getMessage ()); 
        } 
    } 

}

내용 출력

CRON2 작업은 threadId = 57이며, threadName = 작업. 1, 시작 = 2019년 10월 11일 17시 39분 53초 시작
cron1 작업이 시작 = 2019년 10월 11일 17시 39분 54초, threadId = 시작 59 threadName = 작업을 -2
CRON2 작업 = 시작 2019년 10월 11일 17시 39분 58초 시작 상기 threadId = 61이고, threadName = 태스크. -3-
CRON2 작업이 시작 = 시작 2019년 10월 11일 17시 40분 3초, threadId = 63 threadName = 작업-4
스레드 이름 CRON2 실행 작업 : 작업 1 끝, 시작 = 2019년 10월 11일 17 : 39 : 53, 끝 = 2019년 10월 11일 17시 40분 3초
======== ============
cron1 작업이 시작 = 2019년 10월 11일 17시 40분 4초 상기 threadId = 64 시작 threadName는 = 태스크. -5-
CRON2 작업이 시작 = 시작 2019년 10월 11일 17 40 : 08, threadId = 65 , = threadName 태스크 -6-
태스크 실행 스레드 이름 CRON2 : 태스크 -3- 단부를 시작 2019년 10월 11일 = 17 : 39 : 58 = 단부 2019년 10월 11일 17시 40분 : 08
====================
CRON2 작업이 시작, 시작 = 2019년 10월 11일 17시 40분 13초, threadId = 66, threadName = 작업-7
스레드 이름 CRON2 실행 작업 : 작업-4 끝, 시작 = 2019년 10월 11일 17 : 40 : 03, 종료 = 2019년 10월 11일 17시 40분 13초
============ ========
스레드 이름 cron1 실행 작업 : 39 : 태스크 2 끝 = 2019년 10월 11일 17 시작 (54), 끝 = 2019년 10월 11일 17시 39분 54초

설명 : 

  여러 스레드를 열 @EnableAsync

  비동기 작업으로 표시 @Async

  타이밍 작업의 각각은 다른 스레드에서 처리, 스레드 이름 접두사는 task-되었다

  기본값은 10 개 스레드입니다

구성을 수정

spring.task.execution.thread 이름 접두사 = mytask- 
spring.task.execution.pool.core -size = 5

다시 실행 출력

CRON2 태스크 56 threadId = - = MyTask threadName이다 시작 = 2019년 10월 11일 17시 44분 0초 시작한다. 1 개
cron1 작업이 시작 = 시작 2019년 10월 11일 17시 44분 1초, threadId = 57 = threadName mytask -2
CRON2 작업 = 2019년 10월 11일 17시 44분 5초 상기 threadId = 58 = MyTask-threadName를 시작 시작한다. 3
CRON2 작업이 시작 = 시작 2019년 10월 11일 17시 44분 10초, threadId = 59 threadName = mytask-4
작업 실행 스레드 이름 CRON2 : = 2019년 10월 11일 17 시작 mytask-1 엔드 : 44 : 00, 끝 = 2019년 10월 11일 17시 44분 10초을
======== ============
cron1 작업이 시작, 시작 = 2019년 10월 11일 17시 44분 11초, threadId = 60, threadName = mytask-5
스레드 이름이 작업을 실행 CRON2 : mytask-3 끝을 05, 끝 = 2019년 10월 11일 17시 44분 15초 : 44 : 2019년 10월 11일 17 = 시작
==============
, CRON2 작업 시작 2019년 10월 11일 17시 44분 15초 = 3 threadId = 58 = myTask-threadName 시작.
CRON2 작업이 시작 = 시작 2019년 10월 11일 17시 44분 20초, threadId = 56 = threadName mytask-1
스레드 이름은 실행중인 작업 CRON2 : mytask-4 엔드를 시작 = 2019년 10월 11일 17 : 44 : 10, 종료 = 2019년 10월 11일 17시 44분 20초
============ ========
태스크 실행 스레드 이름 cron1 : 44 : mytask-2 단부는 2019년 10월 11일 = 17 시작 1 단부 = 2019년 10월 11일 17시 44분 1초

 

 

 

 

 

추천

출처www.cnblogs.com/baby123/p/11655956.html