Ⅰ 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()方法...
}