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

  • SEARCH
  • Home
  • 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 3317452
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:35:21+00:00 2026-05-17T22:35:21+00:00

From reading on Stack Overflow I’ve seen that many of you don’t recommend using

  • 0

From reading on Stack Overflow I’ve seen that many of you don’t recommend using Timer Task. Hmmm… but I already implemented this:

I have this code:

detectionHandlerTimer.schedule(myTimerTask, 60 * 1000, 60 * 1000);

The thing is that work of myTimerTask lasts some time.

I would like this behavior:

  1. wait 60 sec.
  2. do task for some time (e.g. 40 – 100 sec).
  3. task finished.
  4. wait 60 seconds.
  5. do task for some time (e.g. 40 – 100 sec).

But the code above behaves like this

  1. wait 60 sec.
  2. do task for some time (e.g. 40 – 100 sec).
  3. task finished
  4. do task for some time (e.g. 40 – 100 sec).

Because the time duration of task is bigger than 60, timer starts task immediately after task is finished. But I would like it to wait again.

  • 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-17T22:35:22+00:00Added an answer on May 17, 2026 at 10:35 pm

    This works. The key is to have the task itself (after it completes) schedule the next occurrence of the task.

    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TaskManager {
    
        private Timer timer = new Timer();
    
        public static void main(String[] args) {
            TaskManager manager = new TaskManager();
            manager.startTask();
        }
    
        public void startTask() {
            timer.schedule(new PeriodicTask(), 0);
        }
    
        private class PeriodicTask extends TimerTask {
            @Override
            public void run() {
                System.out.println(System.currentTimeMillis() + " Running");
    
                /* replace with the actual task */
                try {
                    Thread.sleep(15 * 1000);
                } catch(InterruptedException e) {
                    e.printStackTrace();
                }
                /* end task processing */
    
                System.out.println(System.currentTimeMillis() + " Scheduling 10 seconds from now");
                timer.schedule(new PeriodicTask(), 10 * 1000);
            }
        }
    }
    

    Which prints:

    $ javac TaskManager.java && java TaskManager
    1288282514688 Running
    1288282529711 Scheduling 10 seconds from now
    1288282539712 Running
    1288282554713 Scheduling 10 seconds from now
    1288282564714 Running
    

    Here’s what it looks like if you extract the second components of the timestamps (for clarity):

    $ javac TaskManager.java && java TaskManager
    14                                    Running
    29 (+15 seconds execution)            Scheduling 10 seconds from now
    39 (+10 seconds delay until next run) Running
    54 (+15 seconds execution)            Scheduling 10 seconds from now
    64 (+10 seconds delay until next run) Running
    

    Just replace the 10s with 60s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am busy building an application in which I am reading data from more
I'm Programing in C++, and I have 3 classes that inherit from each other:
i have a problem with reading from a file, and converting content to double.
Reviewing a stack trace of a non-responsive web-app, I realized that some of the
What I must do is open a file in binary mode that contains stored
I was reading this StackOverflow question about eager loading which led me to this
Hi all this is my post on stackoverflow. I am normally a lurker and
First word of warning, I believe this is a stackoverflow rather than programmers question,
For one of my projects thats kind of a content-aggregator i'd like to introduce
My question might be, How does one create a single-line horizontal LinearLayout with two

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.