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()); } }
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); } }
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); } }
作用:在等待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 Schedule實作定時任務的方法解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!