zoukankan      html  css  js  c++  java
  • springboot-定时任务-多线程

    1、配置异步线程池

    import java.util.concurrent.Executor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    @Configuration
    @EnableAsync//开启异步事件的支持
    public class AsyncConfig {
    
    	/*
    	 * 此处成员变量应该使用@Value从配置中读取
    	 */
    	private int corePoolSize = 10;
    	private int maxPoolSize = 200;
    	private int queueCapacity = 10;
    
    	@Bean
    	public Executor taskExecutor() {
    		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    		executor.setCorePoolSize(corePoolSize);
    		executor.setMaxPoolSize(maxPoolSize);
    		executor.setQueueCapacity(queueCapacity);
    		executor.initialize();
    		return executor;
    	}
    
    }
    

    2、定时任务类

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    @Component
    @EnableScheduling
    public class ScheduledService {
    
    	private Logger logger = LoggerFactory.getLogger(ScheduledService.class);
    
    	@Scheduled(cron = "0/5 * * * * *")
    	@Async
    	public void scheduled() {
    		logger.info("=====>>>>>使用cron  {}", System.currentTimeMillis());
    	}
    
    	@Scheduled(fixedRate = 5000)
    	@Async
    	public void scheduled1() {
    		logger.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
    	}
    
    	@Scheduled(fixedDelay = 5000)
    	@Async
    	public void scheduled2() {
    		logger.info("=====>>>>>fixedDelay{}", System.currentTimeMillis());
    	}
    }
    

      

  • 相关阅读:
    视频测试序列的下载地址【转】
    RDO、SAD、SATD、λ相关概念【转】
    RGB、YUV和YCbCr介绍【转】
    H.264和HEVC分析软件和工具【转】
    Mysql——Innodb和Myisam概念与数据恢复
    ubuntu个人初始配置记录
    H.264学习笔记6——指数哥伦布编码
    H.264学习笔记5——熵编码之CAVLC
    C/C++语言学习——内存分配管理
    H.264学习笔记4——变换量化
  • 原文地址:https://www.cnblogs.com/lichangyunnianxue/p/9771517.html
Copyright © 2011-2022 走看看