Spring Boot 定时任务单线程和多线程

Spring Boot 的定时任务:

第一种:把参数配置到.properties文件中:

代码:

package com.accord.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 从配置文件加载任务信息
 * @author 王久印
 * 2018年3月1日
 */
@Component
public class ScheduledTask {

  private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

  //@Scheduled(fixedDelayString = "${jobs.fixedDelay}")
  @Scheduled(fixedDelayString = "2000")
  public void getTask1() {
    System.out.println("任务1,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));
  }

  @Scheduled(cron = "${jobs.cron}")
  public void getTask2() {
    System.out.println("任务2,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));
  }
}
application.properties文件:

jobs.fixedDelay=5000
jobs.cron=0/5 * *  * * ?

SpringBootCron2Application.java中:

package com.accord;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringBootCron2Application {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootCron2Application.class, args);
	}
}

注:@EnableScheduling  这个一定要加上;否则,不会定时启动任务!

@Scheduled中的参数说明:

@Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;

@Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;

@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;

@Scheduled(cron="* * * * * ?"):按cron规则执行。
在线Cron表达式生成器:http://cron.qqe2.com/


第二种定时任务:单线程和多线程

1、创建定时任务:

package com.accord.task;

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

/**
 * 构建执行定时任务
 * @author 王久印
 * 2018年3月1日
 * TODO
 */
@Component
public class ScheduledTask2 {

    private Logger logger = LoggerFactory.getLogger(ScheduledTask2.class);

    private int fixedDelayCount = 1;
    private int fixedRateCount = 1;
    private int initialDelayCount = 1;
    private int cronCount = 1;

    @Scheduled(fixedDelay = 5000)        //fixedDelay = 5000表示当前方法执行完毕5000ms后,Spring scheduling会再次调用该方法
    public void testFixDelay() {
        logger.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
    }

    @Scheduled(fixedRate = 5000)        //fixedRate = 5000表示当前方法开始执行5000ms后,Spring scheduling会再次调用该方法
    public void testFixedRate() {
        logger.info("===fixedRate: 第{}次执行方法", fixedRateCount++);
    }

    @Scheduled(initialDelay = 1000, fixedRate = 5000)   //initialDelay = 1000表示延迟1000ms执行第一次任务
    public void testInitialDelay() {
        logger.info("===initialDelay: 第{}次执行方法", initialDelayCount++);
    }

    @Scheduled(cron = "0 0/1 * * * ?")  //cron接受cron表达式,根据cron表达式确定定时规则
    public void testCron() {
        logger.info("===initialDelay: 第{}次执行方法", cronCount++);
    }

}

使用 @Scheduled来创建定时任务 这个注解用来标注一个定时任务方法。 
通过看 @Scheduled源码可以看出它支持多种参数:
    (1)cron:cron表达式,指定任务在特定时间执行;
    (2)fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
    (3)fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;
    (4)fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
    (5)fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
    (6)initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
    (7)initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
    (8)zone:时区,默认为当前时区,一般没有用到。

2、开启定时任务:

package com.accord;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringBootCron2Application {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootCron2Application.class, args);
	}
}

注:这里的 @EnableScheduling  注解,它的作用是发现注解 @Scheduled的任务并由后台执行。没有它的话将无法执行定时任务。
引用官方文档原文:
@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

3、执行结果(单线程)

就完成了一个简单的定时任务模型,下面执行springBoot观察执行结果:


从控制台输入的结果中我们可以看出所有的定时任务都是在同一个线程池用同一个线程来处理的,那么我们如何来并发的处理各定时任务呢,请继续向下看。

4、多线程处理定时任务:

看到控制台输出的结果,所有的定时任务都是通过一个线程来处理的,我估计是在定时任务的配置中设定了一个SingleThreadScheduledExecutor,于是我看了源码,从ScheduledAnnotationBeanPostProcessor类开始一路找下去。果然,在ScheduledTaskRegistrar(定时任务注册类)中的ScheduleTasks中又这样一段判断:

if (this.taskScheduler == null) {
	this.localExecutor = Executors.newSingleThreadScheduledExecutor();
	this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
}
这就说明如果taskScheduler为空,那么就给定时任务做了一个单线程的线程池,正好在这个类中还有一个设置taskScheduler的方法:

public void setScheduler(Object scheduler) {
	Assert.notNull(scheduler, "Scheduler object must not be null");
	if (scheduler instanceof TaskScheduler) {
		this.taskScheduler = (TaskScheduler) scheduler;
	}
	else if (scheduler instanceof ScheduledExecutorService) {
		this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));
	}
	else {
		throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());
	}
}
这样问题就很简单了,我们只需用调用这个方法显式的设置一个ScheduledExecutorService就可以达到并发的效果了。我们要做的仅仅是实现SchedulingConfigurer接口,重写configureTasks方法就OK了;

package com.accord.task;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executors;

/**
 * 多线程执行定时任务
 * @author 王久印
 * 2018年3月1日
 */
@Configuration
//所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //设定一个长度10的定时任务线程池
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }
}
5、执行结果(并发)


通过控制台输出的结果看出每个定时任务都是在通过不同的线程来处理了。

  • 38
    点赞
  • 202
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
### 回答1: Spring Boot支持多线程编程,主要有以下几种方式: 1. 继承Thread类或实现Runnable接口 这是最基本的多线程编程方式,可以在Spring Boot中使用。开发者可以通过继承Thread类或实现Runnable接口,重写run()方法实现多线程逻辑。示例代码: ```java public class MyThread extends Thread { @Override public void run() { // 多线程逻辑 } } public class MyRunnable implements Runnable { @Override public void run() { // 多线程逻辑 } } // 使用 new MyThread().start(); new Thread(new MyRunnable()).start(); ``` 2. 使用线程池 线程池可以有效地管理多个线程,避免创建和销毁线程的开销。Spring Boot提供了ThreadPoolTaskExecutor类,可以用来创建和管理线程池。示例代码: ```java @Configuration @EnableAsync public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(20); executor.setQueueCapacity(30); executor.initialize(); return executor; } } // 使用 @Async public void doSomething() { // 多线程逻辑 } ``` 3. 使用CompletableFuture CompletableFuture是Java 8引入的异步编程方式,可以很方便地实现多线程编程。Spring Boot也提供了对CompletableFuture的支持。示例代码: ```java public CompletableFuture<String> doSomething() { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { // 多线程逻辑 return "result"; }); return future; } ``` 以上是Spring Boot多线程编程的三种方式,开发者可以根据具体的业务场景选择不同的方式。 ### 回答2: Spring Boot是一个用于快速构建基于Spring框架的应用程序的工具。在Spring Boot中,可以轻松地实现多线程的功能。 Spring Boot提供了多种方式来实现多线程。一种常用的方式是使用`@Async`注解来标记一个方法为异步方法。在使用`@Async`注解之后,该方法将会在一个单独的线程中执行,而不会阻塞主线程。通过使用这个注解,可以在应用程序中执行耗时操作,而不会影响其他的业务逻辑。需要注意的是,要实现异步方法,还需要在应用程序的主类上添加`@EnableAsync`注解。 除了使用`@Async`注解外,Spring Boot还提供了`ThreadPoolTaskExecutor`类来方便地创建线程池。通过配置线程池的大小、最大线程数等参数,可以实现更加灵活和高效的多线程处理。可以在应用程序的配置文件中添加以下配置来创建线程池: ```java @Configuration @EnableAsync public class AsyncConfig { @Bean(name = "taskExecutor") public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(10); executor.setQueueCapacity(25); executor.setThreadNamePrefix("taskExecutor-"); executor.initialize(); return executor; } } ``` 上述配置创建了一个线程池,核心线程数为5,最大线程数为10,队列容量为25。通过将`@Async`注解和`taskExecutor`作为参数添加到方法中,即可实现多线程的功能。 综上所述,Spring Boot提供了多种方式来实现多线程。使用`@Async`注解可以快速实现异步方法,而`ThreadPoolTaskExecutor`类则提供了更加灵活和高效的线程池配置。这些功能可以帮助我们提高应用程序的性能和并发处理能力。 ### 回答3: Spring Boot多线程允许开发者在应用程序中使用并发处理来提高性能和效率。它基于Java的多线程机制,但通过Spring Boot可以更加方便地进行配置和管理。 Spring Boot使用了Java的Executor框架来处理线程池和线程管理。开发者可以通过@EnableAsync注解启用异步方法和@Async注解将方法标记为异步执行。这样在调用该方法时,Spring Boot会自动创建一个新的线程执行该方法,而当前线程则不会被阻塞。 使用多线程可以提高应用程序的响应性能,特别是在处理一些耗时的操作时。通过异步方法,可以将一些需要等待的操作转移到后台线程中执行,不影响主线程继续执行其他操作。这对于处理大量请求或者IO密集型的任务非常有用。 在配置多线程时,开发者可以指定线程池的大小、最大线程数、线程的生命周期等参数。这些参数可以根据具体的应用场景进行调整,以达到最佳的性能和资源利用。同时,Spring Boot还提供了一些方便的工具类和注解,用于处理线程间的数据共享和同步,如通过ThreadLocal存储线程局部变量,通过@Lock注解实现对指定资源的加锁。 总之,Spring Boot多线程提供了一种便捷的方式来管理并发处理,使得开发者能够更加轻松地实现并行执行和异步操作。它可以大大提高应用程序的性能和响应能力,适用于处理一些耗时的任务和IO密集型的操作。但同时也需要注意线程安全和资源管理的问题,合理配置和使用多线程,才能充分发挥其潜力。
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值