Java实现Timer的定时调度函数schedule的四种用法
schedule的四种用法
schedule(task,time)
task-所安排的任务 time-执行任务的时间
作用:在时间等于或者超过time的时候执行且仅执行一次
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());
}
}
schedule(task,time,period)
task-所要安排执行的任务 time-首次执行任务的时间 period-执行一次task的时间间隔,单位毫秒
作用:时间等于或者超过time首次执行task,之后每隔period毫秒重复执行一次任务
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);
}
}
schedule(task,delay)
task-所要安排的任务 delay-执行任务前的延迟时间,单位毫秒
作用:等待delay毫秒后执行仅执行一次task
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);
}
}
schedule(task, delay,period)
作用:在等待delay毫秒后首次执行task,每隔period毫秒重复执行task
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);
}
}
到此这篇关于Java实现Timer的定时调度函数schedule的四种用法的文章就介绍到这了,更多相关Java schedule内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章