Home > Java > javaTutorial > Analysis of methods to implement scheduled tasks using Java Schedule

Analysis of methods to implement scheduled tasks using Java Schedule

王林
Release: 2023-05-07 21:46:15
forward
1286 people have browsed it

schedule(task, time)

task-the scheduled task time-the time to execute the task

Function: execute when the time is equal to or exceeds time and only execute once

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
 
public class MyTimerTask extends TimerTask {
    private Integer cout = 0;
    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println("Current Time:"+format.format(calendar.getTime()));//获取当前系统时间
        System.out.println("NO.1");
    }
    public static void main(String[] args) {
        MyTimerTask task = new MyTimerTask();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println(format.format(calendar.getTime()));
        calendar.add(Calendar.SECOND,3);//获取距离当前时间3秒后的时间
        Timer timer = new Timer();
        timer.schedule(task,calendar.getTime());
 
    }
}
Copy after login

schedule(task, time, period)

task-the task to be scheduled for execution time-the time when the task is first executed period-the time interval for executing a task, in milliseconds

Function: Execute the task for the first time when the time is equal to or exceeds time, and then execute the task repeatedly every period milliseconds

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
 
public class MyTimerTask extends TimerTask {
    private Integer cout = 0;
    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println("Current Time:"+format.format(calendar.getTime()));//获取当前系统时间
        System.out.println("NO.1");
    }
    public static void main(String[] args) {
        MyTimerTask task = new MyTimerTask();
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println(format.format(calendar.getTime()));
        calendar.add(Calendar.SECOND,3);//获取距离当前时间3秒后的时间
        Timer timer = new Timer();
       //timer.schedule(task,calendar.getTime());
        timer.schedule(task,calendar.getTime(),2000);
    }
}
Copy after login

schedule(task,delay)

task-the task to be scheduled delay-before executing the task Delay time, unit milliseconds

Function: Execute task only once after waiting for delay milliseconds

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
 
public class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println("Current Time:"+format.format(calendar.getTime()));//获取当前系统时间
        System.out.println("NO.1");
    }
    public static void main(String[] args) {
        MyTimerTask task = new MyTimerTask();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println(format.format(calendar.getTime()));
        //calendar.add(Calendar.SECOND,3);//获取距离当前时间3秒后的时间
        Timer timer = new Timer();
        //timer.schedule(task,calendar.getTime());
        //timer.schedule(task,calendar.getTime(),2000);
        timer.schedule(task,2000);
    }
}
Copy after login

schedule(task, delay,period)

Function: Wait for delay milliseconds After executing the task for the first time, execute the task repeatedly every period milliseconds

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
 
public class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println("Current Time:"+format.format(calendar.getTime()));//获取当前系统时间
        System.out.println("NO.1");
    }
    public static void main(String[] args) {
        MyTimerTask task = new MyTimerTask();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        System.out.println(format.format(calendar.getTime()));
        //calendar.add(Calendar.SECOND,3);//获取距离当前时间3秒后的时间
        Timer timer = new Timer();
        //timer.schedule(task,calendar.getTime());
        //timer.schedule(task,calendar.getTime(),2000);
        //timer.schedule(task,2000);
        timer.schedule(task,2000,3000);
    }
}
Copy after login

The above is the detailed content of Analysis of methods to implement scheduled tasks using Java Schedule. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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