您好,登錄后才能下訂單哦!
定時(shí)/計(jì)劃功能主要使用的就是Timer對(duì)象,它在內(nèi)部還是使用多線程的方式進(jìn)行處理,所以它和線程技術(shù)還是有非常大的關(guān)聯(lián)。
Timer類主要作用就是設(shè)置計(jì)劃任務(wù),但封裝任務(wù)的類卻是TimerTask類。TimerTask類是一個(gè)抽象類。
執(zhí)行任務(wù)的時(shí)間晚于當(dāng)前時(shí)間-----在未來(lái)執(zhí)行的效果
import java.util.Date;
import java.util.TimerTask;
public class MyTask extends TimerTask{
@Override
public void run() {
System.out.println("任務(wù)執(zhí)行了, 時(shí)間為:" + new Date());
}
}
public class Test1 {
public static void main(String[] args){
System.out.println("當(dāng)前時(shí)間為: " + new Date());
Calendar calendarRef = Calendar.getInstance();
calendarRef.add(Calendar.SECOND, 10);
Date runDate = calendarRef.getTime();
MyTask task = new MyTask();
// 查看Timer構(gòu)造方法,創(chuàng)建一個(gè)Timer就是啟動(dòng)一個(gè)新的線程
Timer timer = new Timer();
timer.schedule(task, runDate); // 定時(shí)器在某個(gè)時(shí)間(runDate)執(zhí)行某個(gè)任務(wù)(task)
}
}
Timer中允許有多個(gè)TimerTask任務(wù)及延時(shí)的測(cè)試
TimerTask是以隊(duì)列的方式一個(gè)一個(gè)被順序性地執(zhí)行,所以執(zhí)行時(shí)間有可能和預(yù)期的時(shí)間不一致,因?yàn)榍懊娴娜蝿?wù)有可能消耗的時(shí)間較長(zhǎng),則后面的任務(wù)運(yùn)行的時(shí)間也被延后。
import java.util.Date;
import java.util.TimerTask;
public class MyTaskA extends TimerTask{
@Override
public void run() {
try {
System.out.println("A begin timer, 時(shí)間為:" + new Date());
Thread.sleep(20000);
System.out.println("A end timer, 時(shí)間為:" + new Date());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.Date;
import java.util.TimerTask;
public class MyTaskB extends TimerTask{
@Override
public void run() {
System.out.println("B begin timer, 時(shí)間為:" + new Date());
System.out.println("B end timer, 時(shí)間為:" + new Date());
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class Test1 {
public static void main(String[] args){
System.out.println("當(dāng)前時(shí)間為: " + new Date());
Calendar calendarRef1 = Calendar.getInstance();
Date runDate1 = calendarRef1.getTime();
System.out.println("A計(jì)劃時(shí)間為: " + runDate1);
Calendar calendarRef2 = Calendar.getInstance();
calendarRef2.add(Calendar.SECOND, 10);
Date runDate2 = calendarRef2.getTime();
System.out.println("B計(jì)劃時(shí)間為: " + runDate2);
MyTaskA task1 = new MyTaskA();
MyTaskB task2 = new MyTaskB();
Timer timer = new Timer();
timer.schedule(task1, runDate1); // 定時(shí)器在某個(gè)時(shí)間(runDate)執(zhí)行某個(gè)任務(wù)(task)
timer.schedule(task2, runDate2);
}
}
schedule(TimerTask task, Date firstTime, long period)
該方法的作用是在指定的日期之后按指定的間隔周期,無(wú)限循環(huán)地執(zhí)行某一任務(wù)
import java.util.Date;
import java.util.TimerTask;
public class MyTask extends TimerTask{
@Override
public void run() {
System.out.println("任務(wù)開(kāi)始: "+new Date());
System.out.println("mytask...");
System.out.println("任務(wù)結(jié)束: "+new Date());
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class Run {
public static void main(String[] args){
System.out.println("當(dāng)前時(shí)間為:"+new Date());
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, 10);
Date runDate1 = c.getTime();
System.out.println("計(jì)劃時(shí)間為:"+runDate1);
MyTask myTask = new MyTask();
Timer timer = new Timer();
timer.schedule(myTask, runDate1, 10000);
}
}
TimerTask類的cancel()方法
TimerTask類中的cancel()方法的作用是將自身從任務(wù)隊(duì)列中進(jìn)行清除
import java.util.Date;
import java.util.TimerTask;
public class MyTaskA extends TimerTask{
@Override
public void run() {
System.out.println("A run timer="+new Date());
this.cancel();
System.out.println("A任務(wù)自己移除自己");
}
}
import java.util.Date;
import java.util.TimerTask;
public class MyTaskB extends TimerTask{
@Override
public void run() {
System.out.println("B run timer="+new Date());
} 鄭州人流醫(yī)院 http://m.zzzy120.com/
}
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class Run {
public static void main(String[] args){
System.out.println("當(dāng)前時(shí)間為:"+new Date());
Calendar c = Calendar.getInstance();
Date runDate1 = c.getTime();
System.out.println("計(jì)劃時(shí)間為:"+runDate1);
MyTaskA myTaskA = new MyTaskA();
MyTaskB myTaskB = new MyTaskB();
Timer timer = new Timer();
timer.schedule(myTaskA, runDate1, 4000);
timer.schedule(myTaskB, runDate1, 4000);
}
}
Timer的cancel()方法
和TimerTask類中的cancel()方法清除自身不同,Timer類中的cancel()方法作用是將任務(wù)隊(duì)列中全部的任務(wù)進(jìn)行清空。
import java.util.Date;
import java.util.TimerTask;
public class MyTaskA extends TimerTask{
@Override
public void run() {
System.out.println("A run timer="+new Date());
}
}
import java.util.Date;
import java.util.TimerTask;
public class MyTaskB extends TimerTask{
@Override
public void run() {
System.out.println("B run timer="+new Date());
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class Run {
public static void main(String[] args) throws InterruptedException{
System.out.println("當(dāng)前時(shí)間為:"+new Date());
Calendar c = Calendar.getInstance();
Date runDate1 = c.getTime();
System.out.println("計(jì)劃時(shí)間為:"+runDate1);
MyTaskA myTaskA = new MyTaskA();
MyTaskB myTaskB = new MyTaskB();
Timer timer = new Timer();
timer.schedule(myTaskA, runDate1, 2000);
timer.schedule(myTaskB, runDate1, 2000);
Thread.sleep(10000);
timer.cancel();
}
}
schedule(TimerTask task, long delay)
該方法的作用是以執(zhí)行schedule(TimerTask task, long delay)方法當(dāng)前的時(shí)間為參考時(shí)間,在此時(shí)間基礎(chǔ)上延遲指定的毫秒數(shù)后執(zhí)行一次TimerTask任務(wù)
import java.util.Date;
import java.util.TimerTask;
public class MyTask extends TimerTask{
@Override
public void run() {
System.out.println("A run timer="+new Date());
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class Run {
public static void main(String[] args) throws InterruptedException{
System.out.println("當(dāng)前時(shí)間為:"+new Date());
Calendar c = Calendar.getInstance();
Date runDate1 = c.getTime();
System.out.println("計(jì)劃時(shí)間為:"+runDate1);
MyTask myTask = new MyTask();
Timer timer = new Timer();
timer.schedule(myTask, 7000); // 計(jì)劃任務(wù)延遲7秒后執(zhí)行
Thread.sleep(10000);
timer.cancel();
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。