Timer的定时调度函数schedule的四种用法(Java)

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)