Home > Java > javaTutorial > Detailed introduction to the code example of scheduled task taskScheduler in spring

Detailed introduction to the code example of scheduled task taskScheduler in spring

黄舟
Release: 2017-03-06 10:32:46
Original
2146 people have browsed it

This article mainly introduces the relevant information about the scheduled task taskScheduler in spring. The article introduces it in detail through sample code. I believe it has certain reference value for everyone. Friends in need can take a look below.

Preface

As we all know, after the spring version 3.0, it comes with a scheduled task tool, and it is simple and convenient to use, without the need for configuration files. Dynamically change execution status. You can also use cron expressions to set up scheduled tasks.

The executed class must implement the Runnable interface

TaskScheduler interface

TaskScheduler It is an interface. The TaskScheduler interface defines 6 methods


1, schedule(Runnable task, Trigger trigger);

Specify a trigger to execute a scheduled task. You can use CronTrigger to specify Cron expressions and execute scheduled tasks

 CronTrigger t = new CronTrigger("0 0 10,14,16 * * ?");
 taskScheduler.schedule(this, t);
Copy after login

2、schedule(Runnable task, Date startTime);

Specify a specific time point to execute the scheduled task. You can dynamically specify the time and start the task. Only executed once. (Much easier to use than Timer. I wish I had discovered this interface earlier...)

3. scheduleAtFixedRate(Runnable task, long period);

Immediate execution, circular task, specify an execution cycle (millisecond timing)

PS:No matter whether the previous cycle is completed or not, until The execution will start in the next cycle

4, scheduleAtFixedRate(Runnable task, Date startTime, long period);

The execution will start at the specified time. Cyclic task, specify an interval period (millisecond timing)

PS:No matter whether the previous cycle is completed or not, the next cycle will start execution when the time is up

5, scheduleWithFixedDelay(Runnable task, long delay);

Execute immediately, cycle the task, specify an interval period (millisecond timing)

PS:After the execution of the previous cycle, wait for the delay time, and then start the execution of the next cycle

6, scheduleWithFixedDelay(Runnable task, Date startTime, long delay);

Specify the time to start execution, cycle the task, specify an interval period (millisecond timing)

PS:After the execution of the previous cycle, wait for the delay time, and start the execution of the next cycle

There are five implementation classes under TaskScheduler


1. ConcurrentTaskScheduler

Execute tasks in the current thread. If the task is simple, you can use this class to perform it directly. fast and convenient.

PS:This is a single-threaded operation

public class LocTest implements Runnable {
 private ConcurrentTaskScheduler tpts = new ConcurrentTaskScheduler();
 private void start() {
tpts.schedule(this, new Date());
 }
 public void run() {
Thread ct = Thread.currentThread();
System.out.println("current id:"+ct.getId());
System.out.println("current name:"+ct.getName());
 }
 public static void main(String[] args) {
new LocTest().start();
 }
}
Copy after login

2, DefaultManagedTaskScheduler

Execute tasks in the current thread. This is a subclass of ConcurrentTaskScheduler and adds JNDI support. The same usage as ConcurrentTaskScheduler, you need to use JNDI to set up the default implementation class of

3 and ThreadPoolTaskScheduler

TaskScheduler interface separately, and execute multi-threaded scheduled tasks. You can set the number of execution thread pools (default is one thread).

PS:

  1. Must be called before use initialize() [Initialization method]

  2. There is a shutDown() method, you can close the thread after execution

public class LocTest implements Runnable {
private ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
private void start() {
taskScheduler.setPoolSize(10);
//必须得先初始化,才能使用
taskScheduler.initialize();
taskScheduler.schedule(this, new Date());
}
public void run() {
Thread ct = Thread.currentThread();
System.out.println("current id:"+ct.getId());
System.out.println("current name:"+ct.getName());
}
public static void main(String[] args) {
new LocTest().start();
}
}
Copy after login

4. TimerManagerTaskScheduler

I haven’t used this yet, so I don’t know it in detail. I will add it when I have time. Anyone who knows more is welcome to add.

Summary

The above is a detailed introduction to the code example of the scheduled task taskScheduler in spring Content, for more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template