67,515
社区成员
发帖
与我相关
我的任务
分享
mapTimer.put("T"+dct.getCdid().getCdid().toString(),new Timer());
mapTimer.get("T"+dct.getCdid().getCdid().toString()).scheduleAtFixedRate(ctt, delaytime, cycletime);
mapTimer.get("T"+dct.getCdid().getCdid().toString()).cancel();
mapTimer.remove("T"+dct.getCdid().getCdid().toString());
mapTimer.put("T"+dct.getCtid().toString(),new Timer());
mapTimer.get("T"+dct.getCtid().toString()).scheduleAtFixedRate(ctt, delaytime, cycletime);
mapTimer.get("T"+dct.getCtid().toString()).cancel();
mapTimer.remove("T"+dct.getCtid().toString());
public class CleanTimer {
private static Map<String,Timer> mapTimer=new HashMap<String,Timer>();
private CleanTimer() {
}
public static void StartService(String taskName) {
try{
mapTimer.put(taskName,new Timer());
CleanTimerTask ctt=new CleanTimerTask(taskName);
mapTimer.get(taskName).scheduleAtFixedRate(ctt, 0, 1000);
} catch(Exception ex) {
ex.printStackTrace();
}
}
public static void RemoveTimer(String taskName){
try{
mapTimer.get(taskName).cancel();
mapTimer.remove(taskName);
}catch(Exception e){
e.printStackTrace();
}
}
}
public class CleanTimerTask extends TimerTask {
private String taskName;
public CleanTimerTask(String taskName) {
this.taskName = taskName;
}
public void run() {
System.out.println(taskName+" is running.");
}
}
public class CleanTimerDriver {
public static void main(String[] args) throws InterruptedException {
CleanTimer.StartService("Task1");
CleanTimer.StartService("Task2");
Thread.sleep(3000);
CleanTimer.RemoveTimer("Task1");
CleanTimer.RemoveTimer("Task2");
}
}
public class CleanTimer{
private static Map<String,Timer> mapTimer=new HashMap<String,Timer>();
/**
* Creates a new instance of CleanTimer
*/
public CleanTimer() {
}
public static void StartService(DctCleanTab dct,Date delaytime, Long cycletime) {
try{
mapTimer.put("T"+dct.getCdid().getCdid().toString(),new Timer());
CleanTimerTask ctt=new CleanTimerTask();
ctt.setDctParam(dct);
mapTimer.get("T"+dct.getCdid().getCdid().toString()).scheduleAtFixedRate(ctt, delaytime, cycletime);
DctLogger.info(dct.getTaskname()+"任务实例成功启动!");
} catch(Exception ex) {
ex.printStackTrace();
DctLogger.error(dct.getTaskname()+"任务实例启动失败:"+ex.getMessage(),ex.getCause());
}
}
public static void RemoveTimer(DctCleanTab dct){
try{
mapTimer.get("T"+dct.getCdid().getCdid().toString()).cancel();
mapTimer.remove("T"+dct.getCdid().getCdid().toString());
DctLogger.info(dct.getTaskname()+"任务实例停用成功!");
}catch(Exception e){
e.printStackTrace();
DctLogger.error(dct.getTaskname()+"任务实例停用失败:"+e.getMessage(),e.getCause());
}
}
}
public class CleanTimerTask extends TimerTask {
private DctCleanTab dctParam;
/** Creates a new instance of CleanTimerTask */
public CleanTimerTask() {
}
public void run() {
try{
ExecuteCleanBean.ExecuteCleanFunction(dctParam);
System.out.println("StrParam:"+dctParam.getTaskname()+new Date());
} catch(Exception ex) {
ex.printStackTrace();
}
}
public DctCleanTab getDctParam() {
return dctParam;
}
public void setDctParam(DctCleanTab dctParam) {
this.dctParam = dctParam;
}
}
public class CleanTimer{
private Timer timer; //这里有问题
//可改为:
//private List<Timer> timerList = new ArrayList<Timer>();
//在这个基础上,楼主再作相应的修改就可以了。
/**
* Creates a new instance of CleanTimer
*/
public CleanTimer() {
}
public void StartService(Date delaytime, Long cycletime) {
try{
timer = new Timer(); //这里有问题
//每次开始新的任务
//timer会被赋予新值
//原来的timer和它的TimerTask还有线程就无法终止了。
timer.scheduleAtFixedRate(new CleanTimerTask(), delaytime, cycletime);
} catch(Exception ex) {
ex.printStackTrace();
}
}
}