1、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("currentTime:"+format.format(calendar.getTime()));//获取当前系统时间
System.out.println("11111");
}
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()));
Timer timer = new Timer();
timer.schedule(task,2000);
}
}
2、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("22222");
}
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()));
Timer timer = new Timer();
timer.schedule(task,2000,3000);
}
}
3、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("333333");
}
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());
}
}
4、schedule(task,time,period)
参数:task—所要安排执行的任务;time—首次执行任务的时间;period—执行一次task的时间间隔,单位毫秒
作用:时间等于或者超过time首次执行task,之后每隔period毫秒重复执行一次任务
注意:schedule在执行的时候,如果time是小于当前时间,那么会立即执行一次,然后每隔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("44444");
}
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(),2000);
}
}