Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Monthly Timer
    primarykey
    data
    text
    <p>I am trying to create a Timer/TimerTask that will run the same day of every month. I can't schedule a repeating Timer because a month won't always be the same lenght of time. </p> <p>So, here is my solution:</p> <pre><code>public class MyTask extends TimerTask { public void run(){ //do process file stuff if(scheduledExecutionTime() != 0){ TimerHelper.restartMyTimer(); } } } public class TimerHelper { public static HashTable timersTable = new HashTable(); public static void restartMyTimer(){ Calendar runDate = Calendar.getInstance(); runDate.set(Calendar.DAY_OF_MONTH, 1); runDate.set(Calendar.HOUR_OF_DAY, 4); runDate.set(Calendar.MINUTE, 0); runDate.add(Calendar.MONTH, 1);//set to next month MyTask myTask = new MyTask(); Timer myTimer = new Timer(); myTimer.schedule(myTask, runDate.getTime()); timersTable = new HashTable();//keeping a reference to the timer so we timersTable.put("1", myTimer);//have the option to cancel it later } } </code></pre> <p>The problem I think I'm going to run into is that because the first TimerTask creates the second Timer, will the first Timer be kept around because it created the second? After the code finishes on the first Timer, will that thread and object be taken care of by garbage collection? Over time I don't want to build up a bunch of Threads that aren't doing anything but aren't being removed. Maybe I don't have a proper understanding of how Threads and Timers work...</p> <p>I'm open to suggestions of other ways to create a monthly timer as long as I don't have to use third party JARs.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload