Ⅰ java關於Timer計時器的問題求解
timer內部有TimerTask。TimerTask可以取抄消cancel()。取消了,但是還在timer內部。
timer.purge()移除取消了的任務。
所以 最好 cancel 之後調用 purge 然後 置空timer timer =null;
不調用timer.cancel(),timerTask線程會一直被執行,
調用timer.cancel(),timerTask也會執行完當次之後結束。
最好
if(timer!=null){
timer.cancel();
timer.purge();
timer=null;
}
Ⅱ 簡單理解java中timer的schele和scheleAtFixedRate方法的區別
樓主,我們看API文檔,好吧,,,,,參數各有不同
void
schele(TimerTask task,
Date time)
Scheles the specified task for execution at the specified time.
void
schele(TimerTask task, Date firstTime, long period)
Scheles the specified task for repeated fixed-delay execution,
beginning at the specified time.
void
schele(TimerTask task, long delay)
Scheles the specified task for execution after the specified delay.
void
schele(TimerTask task,
long delay, long period)
Scheles the specified task for repeated fixed-delay execution,
beginning after the specified delay.
void
scheleAtFixedRate(TimerTask task,
Date firstTime, long period)
Scheles the specified task for repeated fixed-rate execution,
beginning at the specified time.
void
scheleAtFixedRate(TimerTask task,
long delay, long period)
Scheles the specified task for repeated fixed-rate execution,
beginning after the specified delay.
參數中
delay參數是延時,就是延時多少毫秒後開始。
period 是周期,就是隔多少毫秒後,再執行下一次
time 就是執行的時間,不循環
firstTime 就是首次執行的時間
schele 會等本次執行完畢、再下一次
scheleAtFixedRate 就是不管上一次有沒有執行完畢,都繼續本次、也會按原定時間,執行下一次。。。。。。。。。。
Ⅲ 在java中timertask是什麼類
TimerTask是一個實現了Runnable介面的抽象類,代表一個可以被Timer執行的任務。
Timer類是一種線程設施,可以用來實現某一個時間或某一段時間後安排某一個任務執行一次或定期重復執行。該功能和TimerTask配合使用。TimerTask類用於實現由Timer安排的一次或重復執行的某個任務。每一個Timer對象對應的是一個線程,因此計時器所執行的任務應該迅速完成,否則會延遲後續的任務執行。
void cancel()
// 終止此計時器,丟棄所有當前已安排的任務。
int purge()
//從此計時器的任務隊列中移除所有已取消的任務。
void schele(TimerTask task, Date time)
//安排在指定的時間執行指定的任務。
void schele(TimerTask task, Date firstTime, long period)
//安排指定的任務在指定的時間開始進行重復的固定延遲執行。
void schele(TimerTask task, long delay)
//安排在指定延遲後執行指定的任務。
void schele(TimerTask task, long delay, long period)
//安排指定的任務從指定的延遲後開始進行重復的固定延遲執行。
void scheleAtFixedRate(TimerTask task, Date firstTime, long period)
//安排指定的任務在指定的時間開始進行重復的固定速率執行。
void scheleAtFixedRate(TimerTask task, long delay, long period)
//安排指定的任務在指定的延遲後開始進行重復的固定速率執行。
+++++++++++++++++++++++++++++++++++++++++++++++++++
boolean cancel()
//取消此計時器任務。
abstract void run()
//此計時器任務要執行的操作。
long scheledExecutionTime()
//返回此任務最近實際 執行的已安排 執行時間。
++++++++++++++++++++++++++++++++++++++++++++++++++++++
package zzs.time.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;
public class MyTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat=null;
simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
System.out.println("當前的系統時間為:"+simpleDateFormat.format(new Date()));
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
package zzs.time.demo;
import java.util.Timer;
public class TestTask {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Timer timer=new Timer();
MyTask myTask=new MyTask();
timer.schele(myTask, 1000, 2000);
// Thread.sleep(5000);
//timer.cancel();
}
}
運行結果:
當前的系統時間為:2011-07-10 15:37:44:831
當前的系統時間為:2011-07-10 15:37:46:786
當前的系統時間為:2011-07-10 15:37:48:786
當前的系統時間為:2011-07-10 15:37:50:786
當前的系統時間為:2011-07-10 15:37:52:786
當前的系統時間為:2011-07-10 15:37:54:786
Ⅳ java中每個月第一天執行一次任務的定時器如何實現
Date d = new Date();//獲取伺服器的時間。。。
Calendar c= Canlendar.getInstance();
c.setTime(d);
if(c.get(Calendar.DAY_OF_MONTH) == 1) //當前是1號
{
//拿出黨員的入黨日期(年月)同當前月進行比較 如果相同 發送簡訊
// //寫下你的判斷代碼
}
else //當前不是1號 則從下個月1號開始執行定期任務
{
c.set(Calendar.MONTH,c.get(Calendar.MONTH)+ 1);//設置為下月
c.set(Calendar.DAY_OF_MONTH,1);//設置為下月的1號
Timer timer =new Timer();
timer.scheleAtFixedRate(new TimerTask()
{
public void run()
{
//每天都來判斷一下 如果當前日期是1號
////則拿出黨員的入黨日期(年月)同當前月進行比較 如果相同 發送簡訊
//run函數里寫下你的判斷代碼
}},c.getTime(),24* 3600*1000); //每天執行一次run()方法...
}