Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3348432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:32:31+00:00 2026-05-18T01:32:31+00:00

I am trying to create a Timer/TimerTask that will run the same day of

  • 0

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.

So, here is my solution:

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
    }
}

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…

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.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:32:31+00:00Added an answer on May 18, 2026 at 1:32 am

    If what worries you is to create unneeded objects you can alway create an object which in turn creates/”destroy” all the references, so the objects created may be gc’ed.

    In the worst case, you’ll have 12 unneeded objects in a year, which, I think is bearable. Still your concern is valid.

    Here’s my attempt following Joel’s suggestion of schedule at the end of the execution. Notice, the current Timer is replaced by a new one, so, both, the timer and the timer task could be 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();
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a countdown timer that takes countdown, an IBOutlet connected to
I'm trying to create an instance of a class at run time. The classes
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
I am trying to create a timer job in WSS 3.0. My timer job
I have been trying to create a timer program with VB 2010 to the
I'm trying to create a simple flex4 project which involves some timers that trigger
I'm trying to create an Apache Camel route that sends a jms message to
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.