spring boot 2X in @Scheduled achieve timing task and multi-thread configuration

@Scheduled can be easily achieved using the regular tasks

Version of spring boot of 2.1.6.RELEASE

package com.abc.demo.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

@EnableScheduling
@Component
public class ScheduleSetting {

    private final Logger logger = LoggerFactory.getLogger(Tasks.class);

    @Scheduled(fixedRate = 10000, initialDelay = 2000)
    public void scheduleRead() {
        try {
            long timeStamp = System.currentTimeMillis();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Thread thread = Thread.currentThread();
            System.out.println("cron1任务开始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName());
            long endStamp = System.currentTimeMillis();
            try {
                TimeUnit.SECONDS.sleep ( 20 is ); 
            } the catch (InterruptedException E) { 
                e.printStackTrace (); 
            } 
            System.out.println ( "Name cron1 thread running task:" + thread.getName () + "end, start = "+ simpleDateFormat.format (timeStamp) +", End = "+ simpleDateFormat.format (endStamp)); 
            System.out.println ( " +++++++++++++++++++ +++++ " ); 
        } the catch (Exception E) { 
            logger.error (e.getMessage ()); 
        } 
    } 

    @Scheduled (fixedRate = 5000,initialDelay = 1000)
    public void scheduleConvert() {
        try {

            long timeStamp = System.currentTimeMillis();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Thread thread = Thread.currentThread();
            System.out.println("cron2任务开始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName());
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace (); 
            } 
            Long endStamp = System.currentTimeMillis (); 
            System.out.println ( "Name cron2 thread running task:" + thread.getName () + "end, start =" + simpleDateFormat.format ( timeStamp) + ", End =" + simpleDateFormat.format (endStamp)); 
            System.out.println ( "====================" ); 
        } the catch ( E Exception) { 
            logger.error (e.getMessage ()); 
        } 
    } 

}

Output content to run

cron2 task begins, start = 2019-10-11 17:31:52, threadId = 34, threadName = scheduling-1
thread name cron2 running tasks: scheduling-1 end, start = 2019-10-11 17:31: 52, End = 2019-10-11 17:32:02
====================
cron1 task begins, start = 2019-10-11 17:32:02 , threadId = 34, threadName = scheduling -1
thread name cron1 running tasks: scheduling-1 end, Start = 2019-10-11 17: 32: 02, end = 2019-10-11 17:32:02
++ ++++++++++++++++++++++
cron2 task begins, start = 2019-10-11 17:32:22, the threadId = 34 is, threadName = Scheduling. 1-
cron2 task running thread names: scheduling-1 ends, start = 2019-10-11 17: 32: 22, end = 2019-10-11 17:32:32

……

Note:

  cron2 after the implementation will be performed cron1

the reason:

  spring default is single-threaded execution task scheduling

  spring timing task default maximum number of threads to run 1multiple tasks execute time there will be problems

1. Configure thread pool

Add in the configuration file in application.properties

# Thread pool size 
spring.task.scheduling.pool.size = 5 
# thread name prefix 
spring.task.scheduling.thread -name-prefix = myScheduling-

Output content becomes

cron2 task begins, Start = 2019-10-11 17:34:48, 34 is the threadId =, = myScheduling-threadName. 1
cron1 task begins, start = 2019-10-11 17:34:49, threadId = 35, threadName = myScheduling -2
thread running task name cron2: myScheduling-1 ends, Start = 2019-10-11. 17: 34 is: 48, end = 2019-10-11 17:34:58
========== ==========
cron2 task begins, start = 2019-10-11 17:34:58, threadId = 34, threadName = myScheduling-1
thread name cron2 running task: myScheduling-1 end, start 2019-10-11. 17 =: 34 is: 58, End = 2019-10-11 17:35:08
====================
cron2 task begins, start = 2019-10-11 17:35:08, threadId = 57, threadName = myScheduling-3
threads running task name cron1: myScheduling-2 ends, start = 2019-10-11 17: 34: 49, end = 2019- 10-11 17:34:49

……

Note:

  Multi-threaded, cron1 and cron2 do not have to wait each other, but the same task or to wait

2. Concurrent

Modify the code

package com.abc.demo.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

@EnableScheduling
@Component
@EnableAsync
public class ScheduleSetting {

    private final Logger logger = LoggerFactory.getLogger(Tasks.class);

    @Async
    @Scheduled(fixedRate = 10000, initialDelay = 2000)
    public void scheduleRead() {
        try {
            long timeStamp = System.currentTimeMillis();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Thread thread = Thread.currentThread();
            System.out.println("cron1任务开始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName());
            long endStamp = System.currentTimeMillis();
            try {
                TimeUnit.SECONDS.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("cron1任务正在运行的线程名称:" + thread.getName() + " 结束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp));
            System.out.println("++++++++++++++++++++++++");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    @Async
    @Scheduled(fixedRate = 5000, initialDelay = 1000)
    public void scheduleConvert() {
        try {

            long timeStamp = System.currentTimeMillis();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Thread thread = Thread.currentThread();
            System.out.println("cron2任务开始,start=" + simpleDateFormat.format(timeStamp) + ", threadId=" + thread.getId() + ", threadName=" + thread.getName());
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long endStamp = System.currentTimeMillis();
            System.out.println("cron2任务正在运行的线程名称:" + thread.getName() + " 结束,start=" + simpleDateFormat.format(timeStamp) + ",end=" + simpleDateFormat.format(endStamp));
            System.out.println("====================");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

}

Content output

cron2 task begins, Start = 2019-10-11 17:39:53, the threadId = 57 is, threadName = Task. 1-
cron1 task begins, start = 2019-10-11 17:39:54, threadId = 59, threadName = task -2
cron2 task begins, start = 2019-10-11 17:39:58, the threadId = 61 is, threadName = task. 3-
cron2 task begins, start = 2019-10-11 17:40:03, threadId = 63, threadName = task-4
thread name cron2 running tasks: task-1 end, Start = 2019-10-11 17: 39: 53, end = 2019-10-11 17:40:03
======== ============
cron1 task begins, start = 2019-10-11 17:40:04, the threadId = 64, threadName = task. 5-
cron2 task begins, start = 2019-10-11 17 : 40: 08, threadId = 65 , threadName = task-6
thread name cron2 running tasks: task-3 ends, start = 2019-10-11 17: 39: 58, end = 2019-10-11 17:40 : 08
====================
cron2 task begins, start = 2019-10-11 17:40:13, threadId = 66, threadName = task-7
Thread name cron2 running tasks: task-4 end, Start = 2019-10-11 17: 40: 03, End = 2019-10-11 17:40:13
============ ========
thread name cron1 running tasks: task-2 end, start = 2019-10-11 17: 39: 54, end = 2019-10-11 17:39:54

Description: 

  @EnableAsync open multiple threads

  @Async mark it as an asynchronous task

  Each of the timing tasks are handled in different threads, thread name prefix became task-

  The default is 10 threads

Change setting

spring.task.execution.thread-name-prefix=mytask-
spring.task.execution.pool.core-size=5

Re-run output

cron2 task begins, Start = 2019-10-11 17:44:00, 56 is the threadId =, = MyTask-threadName. 1
cron1 task begins, start = 2019-10-11 17:44:01, threadId = 57, threadName = mytask -2
cron2 task begins, start = 2019-10-11 17:44:05, the threadId = 58, = MyTask-threadName. 3
cron2 task begins, start = 2019-10-11 17:44:10, threadId = 59, threadName = mytask-4
thread name cron2 running task: mytask-1 end, Start = 2019-10-11 17: 44: 00, end = 2019-10-11 17:44:10
======== ============
cron1 task begins, start = 2019-10-11 17:44:11, threadId = 60, threadName = mytask-5
thread name cron2 running task: mytask-3 end , start = 2019-10-11 17: 44: 05, End = 2019-10-11 17:44:15
====================
cron2 task starts, 2019-10-11 17:44:15 = start, the threadId = 58, = MyTask-threadName. 3
cron2 task begins, start = 2019-10-11 17:44:20, threadId = 56, threadName = mytask-1
Thread name cron2 running task: mytask-4 end, Start = 2019-10-11 17: 44: 10, End = 2019-10-11 17:44:20
============ ========
thread name cron1 running task: mytask-2 end, start = 2019-10-11 17: 44: 01, end = 2019-10-11 17:44:01

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/baby123/p/11655956.html