java定时任务实现的4种方式

文章目录

    • 1. java自带的Timer
    • 2.ScheduledThreadPool-线程池
    • 3.使用注解的形式:@Scheduled
    • 4.使用Quartz定时任务调度器

1. java自带的Timer

Timer timer = new Timer();
       timer.schedule(new TimerTask() {
           @Override
           public void run() {
               System.out.println("Time's up!");

           }
       },3*1000,1000);

2.ScheduledThreadPool-线程池

ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
      scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
          @Override
          public void run() {
              System.out.println("=========================");
          }
      }, 1000, 2000, TimeUnit.MILLISECONDS);

      scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
          @Override
          public void run() {
              System.out.println(System.currentTimeMillis()+"<><>"+System.nanoTime());
          }
      }, 1000, 2000, TimeUnit.MILLISECONDS);

3.使用注解的形式:@Scheduled

@Component
public class SpringScheduled {
 
    @Scheduled(initialDelay = 2000,fixedDelay = 5000)
    public void doSomething() {
        System.out.println("Spring自带的Scheduled执行了=======================");
    }
}

//下面是开启

@SpringBootApplication
@EnableScheduling
public class DemoApplication {
 
    public static void main(String[] args) throws InterruptedException {
        SpringApplication application = new SpringApplication(DemoApplication.class);
        application.addListeners(new ContextRefreshedEventListener());
        application.run(args);
    }
}

4.使用Quartz定时任务调度器

配置
@Configuration
public class QuartzConfig {
 
 
    @Resource
    private ScheduleTask scheduleTask;
 
    /**
     * 配置定时任务1
     * @return
     */
    @Bean(name="firstJobDetail")
    public MethodInvokingJobDetailFactoryBean firstJobDetail(){
        MethodInvokingJobDetailFactoryBean method = new MethodInvokingJobDetailFactoryBean();
        // 为需要执行的实体类对应的对象
        method.setTargetObject(scheduleTask);
        // 需要执行的方法
        method.setTargetMethod("test");
        // 是否并发执行
        method.setConcurrent(false);
        return method;
    }
 
 
    /**
     *   配置触发器1
     *   @param firstJobDetail
     *   @return
     */
    @Bean(name="firstTrigger")
    public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail){
        SimpleTriggerFactoryBean simpleBean = new SimpleTriggerFactoryBean();
        simpleBean.setJobDetail(firstJobDetail);
        // 设置任务启动延迟
        simpleBean.setStartDelay(1000);
        // 每1秒执行一次
        simpleBean.setRepeatInterval(1000);
        //设置重复计数
        //simpleBean.setRepeatCount(0);
        return simpleBean;
    }
 
    /**
     *  配置Scheduler
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactoryBean(Trigger firstTrigger){
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        factoryBean.setTriggers(firstTrigger);
        return factoryBean;
    }
 
 
 
}

要执行的任务


@Component
public class ScheduleTask {
 
   public void test() {
       System.out.println("====================================");
   }
 
}

转载说明:
作者:璀璨英雄
原文链接:https://blog.csdn.net/l848168/article/details/84971331

你可能感兴趣的:(Java)