每个月1日两点开始执行一个程序.因此我设计了这个类。
其中 Sub sub=new Sub();
sub.sendSub();
是运行我要执行的程序的。但问题是,似乎我这个类_timer.schedule(new SubTimerTask(),date); 使用了它后,无法在指定的时间苏醒,我没有看到run()起来。以为我没有看到
public void run(){
System.out.println("++++++++++++++++++");
这句起作用。不知道为什么,我觉得我的代码没有问题,请帮我看看。谢谢了package com.yiz.hcmms.commonCmd.OrderNotice;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import java.util.Timer;
public class SubTimerTask extends TimerTask {
private Timer _timer;
public int _day = 1;
public int _hour =2;
public SubTimerTask(){
System.out.println("-------------------");
}
/**
* Method run
*
*
* @param
*
*/
public void run(){
System.out.println("++++++++++++++++++");
Sub sub=new Sub();
sub.sendSub();
setTimer(_day,_hour,true);
}
public void setTimer(int day, int hour) {
setTimer(day, hour, false);
} public void setTimer(int day, int hour, boolean bgNextMonth) {
_day = day;
_hour = hour; Calendar cale = Calendar.getInstance();
if(bgNextMonth || cale.get(cale.DAY_OF_MONTH) >= day) {//because time of send is the day,send "sub" next month whether over the day set this setting
cale.set(cale.MONTH, cale.get(cale.MONTH) + 1);
}
Date date = new Date(cale.get(cale.YEAR) - 1900, cale.get(cale.MONTH), day, hour, 0);
if(_timer == null) {
_timer = new Timer();
} System.out.println("Set SubBilling Timer["+date+":"+(date.getTime() - System.currentTimeMillis())+"]: " + _timer.toString());
_timer.schedule(new SubTimerTask(),date);
} public void stop() {
this.cancel();
_timer.cancel();
}
public int getDay() {
return _day;
}
public int getHour() {
return _hour;
}
public void setDay(int _day) {
this._day = _day;
}
public void setHour(int _hour) {
this._hour = _hour;
}
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
SubTimerTask s=new SubTimerTask();
s.setTimer(1,2);
}
}
其中 Sub sub=new Sub();
sub.sendSub();
是运行我要执行的程序的。但问题是,似乎我这个类_timer.schedule(new SubTimerTask(),date); 使用了它后,无法在指定的时间苏醒,我没有看到run()起来。以为我没有看到
public void run(){
System.out.println("++++++++++++++++++");
这句起作用。不知道为什么,我觉得我的代码没有问题,请帮我看看。谢谢了package com.yiz.hcmms.commonCmd.OrderNotice;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import java.util.Timer;
public class SubTimerTask extends TimerTask {
private Timer _timer;
public int _day = 1;
public int _hour =2;
public SubTimerTask(){
System.out.println("-------------------");
}
/**
* Method run
*
*
* @param
*
*/
public void run(){
System.out.println("++++++++++++++++++");
Sub sub=new Sub();
sub.sendSub();
setTimer(_day,_hour,true);
}
public void setTimer(int day, int hour) {
setTimer(day, hour, false);
} public void setTimer(int day, int hour, boolean bgNextMonth) {
_day = day;
_hour = hour; Calendar cale = Calendar.getInstance();
if(bgNextMonth || cale.get(cale.DAY_OF_MONTH) >= day) {//because time of send is the day,send "sub" next month whether over the day set this setting
cale.set(cale.MONTH, cale.get(cale.MONTH) + 1);
}
Date date = new Date(cale.get(cale.YEAR) - 1900, cale.get(cale.MONTH), day, hour, 0);
if(_timer == null) {
_timer = new Timer();
} System.out.println("Set SubBilling Timer["+date+":"+(date.getTime() - System.currentTimeMillis())+"]: " + _timer.toString());
_timer.schedule(new SubTimerTask(),date);
} public void stop() {
this.cancel();
_timer.cancel();
}
public int getDay() {
return _day;
}
public int getHour() {
return _hour;
}
public void setDay(int _day) {
this._day = _day;
}
public void setHour(int _hour) {
this._hour = _hour;
}
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
SubTimerTask s=new SubTimerTask();
s.setTimer(1,2);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货