@Scheduled属性
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.springframework.scheduling.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
String CRON_DISABLED = "-";
String cron() default "";
String zone() default "";
// 上一次任务执行结束开始计时
long fixedDelay() default -1L;
String fixedDelayString() default "";
// 上一次任务开始的时候开始计时 比如5*1000. 上一次任务开始后5秒就继续执行定时任务
long fixedRate() default -1L;
String fixedRateString() default "";
long initialDelay() default -1L;
String initialDelayString() default "";
TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
}
/*
测试案例
1\未开启多线程模式 会导致每5秒定时执行的线程任务发生阻塞,线程任务之间时间间隔不为5s
2\注释开启多线程模式,线程池避免线程频繁创建和销毁。但是一旦线程任务大于线程池的线程数,也会造成阻塞
*/
@Configuration
@EnableScheduling
@EnableAsync
public class ScheduleTask {
private AtomicInteger number = new AtomicInteger();
@Async // 开启异步
@Scheduled(fixedRate = 5000 )
public void job(){
LocalTime start = LocalTime.now();
//前面和末尾几个字符串是用来改变打印的颜色的
System.out.println("\033[31;4m" + Thread.currentThread() + " start " + number.incrementAndGet()
+ " @ " + start + "\033[0m");
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(15)*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalTime end = LocalTime.now();
System.out.println(Thread.currentThread() + " end " + number.get() + " @ "+
end + ", seconds cost "+ (ChronoUnit.SECONDS.between(start, end)));
}
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
return taskScheduler;
}
}
参考博客:https://blog.csdn.net/czx2018/article/details/83501945
标签:Scheduled,java,String,default,线程,import,annotation,属性 From: https://www.cnblogs.com/wuzimeimei/p/17203153.html