“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
preface
Brothers, as a front and back end to the development of the rookie, learning time also want to take into account the knowledge of the front and back oh! Today I remembered the point of scheduled task, which has not been used in the actual development, but this should be a necessary knowledge point!
The writing of a scheduled task
Timer
JDK built-in scheduled task execution class; Advantages: easy to use; Disadvantages: One task exception affects the execution of other tasks; If one task takes too long, other tasks will be delayed. Code implementation:
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTask {
public static void main(String[] args){
// Define a scheduled task
TimerTask timeTask = new TimerTask(){
@Override
public void run() {
System.out.println("Timed output"+ new Date()); }};/ / timer
Timer timer = new Timer();
// Add execution task (delay execution for 1s, execute every 3s)
timer.schedule(timeTask,1000.3000); }}Copy the code
ScheduledExecutorService
JDK1.5 built-in apis; Advantages: Compared with the single thread of Timer, ScheduledExecutorServie uses thread pool to execute tasks, so there is no interaction between tasks. Disadvantages: Can not set a specific time and frequency, such as every Friday at 6 PM scheduled execution of a certain task, it can not be achieved; Code implementation:
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledTask {
public static void main(String[] args){
// Create a task queue
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);//10 is the number of threads
// Execute the task
scheduledExecutorService.scheduleAtFixedRate(() ->{
System.out.println("run schedule: " + new Date());
},1.3, TimeUnit.SECONDS);// The command is executed every 3s after 1s}}Copy the code
Spring Task
Spring FrameWork built-in scheduled tasks; Advantages: Specific time can be set, automatic trigger without manual intervention; Disadvantages: Single thread, can not do data storage type scheduled tasks; Code implementation: Spring Boot boot class annotated: @enablescheduling; Add an annotation to the implementation class: @Component, hosting the class to Spring; The addition of Scheduled tasks is annotated with @scheduled, a cron expression stating the frequency and rules of execution;
@SpringBootApplication
@EnableScheduling // Enable a scheduled task
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("This is a test."); }}import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component // Hosting this class to Spring cannot be omitted
public class SpringTask {
// Add a scheduled task
/** * Spring Task requires no manual intervention, SpringBoot automatically loads and executes scheduled tasks after startup. ** Cron expressions are used to declare the frequency and rules of execution */
@Scheduled(cron = "0, 42, 9 * *?")
public void doTask(){
System.out.println("I'm on a scheduled assignment."); }}Copy the code
conclusion
The above several ways are more basic, more advanced ways to add down will continue to explore ~