如果您担心要创建不需要的对象,则可以始终创建一个对象,该对象又会创建/“销毁”所有引用,因此可以对创建的对象进行通用控制。
在最坏的情况下,一年中将有12个不需要的对象,我认为这是可以承受的。您的担心仍然有效。
这是我在执行死刑后按照乔尔的时间表建议进行的尝试。注意,当前的Timer被新的Timer取代,因此,可以对timer和timer任务进行gc’ed操作。
package monthly.schedule;import java.util.Timer;import java.util.TimerTask;import java.util.Date;import java.util.Calendar;public class MonthlyTimer { // What to do private final Runnable whatToDo; // when private final int dayOfMonth; private final int hourOfDay; // The current timer private Timer current = new Timer();//to avoid NPE public void cancelCurrent() { current.cancel();// cancel this execution; current.purge(); // removes the timertask so it can be gc'ed } // create a new instance public static MonthlyTimer schedule( Runnable runnable, int dayOfMonth, int hourOfDay ) { return new MonthlyTimer( runnable, dayOfMonth, hourOfDay ); } private MonthlyTimer(Runnable runnable, int day, int hour ) { this.whatToDo = runnable; this.dayOfMonth = day; this.hourOfDay = hour; schedule(); } // Schedules the task for execution on next month. private void schedule() { // Do you mean like this? cancelCurrent(); current = new Timer(); // assigning a new instance // will allow the previous Timer to be gc'ed current.schedule( new TimerTask() { public void run() { try { whatToDo.run(); } finally { schedule();// schedule for the next month } } } , nextDate() ); } // Do the next date stuff private Date nextDate() { Calendar runDate = Calendar.getInstance(); runDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); runDate.set(Calendar.HOUR_OF_DAY, hourOfDay); runDate.set(Calendar.MINUTE, 0); runDate.add(Calendar.MONTH, 1);//set to next month return runDate.getTime(); }}class UseIt { public static void main( String [] args ) { int the1st = 1; int at16hrs = 16; MonthlyTimer t = MonthlyTimer.schedule( new Runnable() { public void run() { System.out.println( "Hola" ); }}, the1st, at16hrs ); // will print "Hola" every 1st at 16:00 hrs. // if needed you can cancel with: t.cancelCurrent(); }}


