TimerTask.java
package cn.hk.test; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Timer; public class TimerTask extends java.util.TimerTask { //执行的类 private Class clazz; //执行的类中的方法 private String method; //执行的时间,只是这个时间没有spring中的cronExpression表达式强大 private Date executeDate; //加入所有的类 private List<Class> clazzs=null; //所有类中的方法 private List<String> methods=null; //所有执行的时间列表 private List<Date> executeDates=null; private static Timer timer=new Timer(); public TimerTask(Class clazz,String method,Date executeDate){ instances(); clazzs.add(clazz); methods.add(method); executeDates.add(executeDate); } private TimerTask(){} public void start(){ System.out.println(executeDates.get(0)); Date date=new Date(System.currentTimeMillis()); //只有在当前的时间之前的才会执行,因为默认的Timer中的schedule的方法是会在时间已经过了也会执行的 if(date.before(executeDates.get(0))){ timer.schedule(this, executeDates.get(0)); } } /** * 最终运行的方法 */ public void run(){ if(equalsList()){ for( int i=0;i<clazzs.size();i++){ //得到对象 try { Class clazz=clazzs.get(i); String strMethod=methods.get(i); Date executeDate=executeDates.get(i); if(clazz!=null&&strMethod!=null&&executeDate!=null){ clazz=clazzs.get(i).newInstance().getClass(); Method method=clazz.getMethod(methods.get(i), null); System.out.println(method.invoke(clazz.newInstance(),null)); } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 停止所有的方法 */ public void stop(){ clazzs.clear(); methods.clear(); executeDates.clear(); } private void instances(){ clazzs=new ArrayList<Class>(); methods=new ArrayList<String>(); executeDates=new ArrayList<Date>(); } //判断集合中的数据是否是一样多的 private boolean equalsList(){ if(clazzs!=null&&methods!=null&&executeDates!=null){ if((clazzs.size()==methods.size())?(methods.size()==executeDates.size())?true:false:false){ return true; } } return false; } private void addProp(Class clazz,String method,Date executeDate){ if(clazz!=null&&method!=null&&executeDate!=null){ clazzs.add(clazz); methods.add(method); executeDates.add(executeDate); } } }
StartTask.java
package cn.hk.test; import java.text.ParseException; import java.text.SimpleDateFormat; public class StartTask { public static void main(String[] args) throws InterruptedException, ParseException { cn.hk.test.TimerTask timerTask=new cn.hk.test.TimerTask(StartTask.class,"show",format.parse("2012-7-11 10:30:01")); timerTask.start(); timerTask=new cn.hk.test.TimerTask(StartTask.class,"show",format.parse("2012-7-11 10:30:04")); timerTask.start(); } public void show(){;} }