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()); } }
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); } }
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); } }
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); } }
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!