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 7013043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:20:06+00:00 2026-05-27T22:20:06+00:00

This requirement came up in my Android app, but it applies to Java in

  • 0

This requirement came up in my Android app, but it applies to Java in general. My app “does something” every few seconds. I have implemented this as follows (just relevant snippets – not a complete code):

Snippet1:

public class PeriodicTask {

    private boolean running = true;
    private int interval = 5;

    public void startTask(){
        while (running){
            doSomething();
            try{
                Thread.sleep(interval * 1000);
            } catch(InterruptedException e){
                //Handle the exception.
            }
        }
    }

    public void stopTask(){
        this.running = false;
    }

    public void setInterval(int newInterval){
        this.interval = newInterval;
    }
}

The problem with this approach, as you can see, is that setInterval() is not immediately effective. It takes effect only after a previous sleep() has completed.

Since my use case allows the end user to set the interval in fixed steps (of 1 second – from 1 to 60 seconds), I modified the implementation to sleep within a loop; and check for the new interval value every second as follows:

Snippet2:

public class PeriodicTask {

    private boolean running = true;
    private int interval = 5;
    private int loopCounter = 0;

    public void startTask(){
        while (running){
            doSomething();
            try{
                while(loopCounter < interval) {
                    Thread.sleep(1 * 1000);
                    loopCounter ++;
                }
            } catch(InterruptedException e){
                //Handle the exception.
            }
        }
    }

    public void stopTask(){
        this.running = false;
    }

    public void setInterval(int newInterval){
        synchronized (this) {
            this.interval = newInterval;
            if(newInterval < loopCounter){
                loopCounter = 0;
            }
        }
    }
}

Is there a reason to not use this approach?

I recently came across the interrupt() method for this purpose. But, I couldn’t exactly figure out how to use it. For one, the interrupt method, unlike the sleep method is not static. So, what Thread do I interrupt?

public void setInterval(int newInterval){
        this.interval = newInterval;
        //What thread do I call interrupt() on?
    }

Secondly, if I do succeed in interrupting the sleeping Thread, I believe the catch block for the InterruptedException will be executed. However, I will need to call the startTask() again at this point. I am confused regarding the termination of this recursion. I have gone through several questions on SO regarding the use of interrupt(), but couldn’t figure out anything that helps me.

Any pointers?


EDIT- More details on exact requirement:

MY app fetches some values using a REST call every few seconds. The update interval is configurable by the user.

Now, say the update interval has been set to 60 seconds. Snippet1 I posted would work (incorrectly) as follows:

  • Thread goes to sleep for 60 seconds.
  • Now, lets say user changes the update interval to 5 seconds. The thread is still sleeping.
  • The PeriodicTask would see the new update interval only after the 60 seconds have expired.

The exact requirement is that the new update intervals should be effective immediately (or at least no later than 1 second after being set – since that is what the user is likely to perceive anyway).

My Snippet2 and Snippet3 are attempts at achieving this requirement.

  • 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-27T22:20:06+00:00Added an answer on May 27, 2026 at 10:20 pm

    IIRC, in Java you can object.wait() with a timeout. Is this not what you want? If you want to change the timeout from another thread, change some ‘waitValue’ variable and notify(). The thread will then ‘immediately’ run and then wait again with the new timeout value. No explicit sleep required.

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

Sidebar

Related Questions

I think I've come across this requirement for a dozen times. But I could
I realized today that I have blindly just followed this requirement for years without
This is my requirement: I have a bunch of radio box selections (types of
Requirement is this ... We have our 3 web-applications deployed in RHEL-5 server, we
I am working in JTable and I have a requirement like this. Say There
I have a kind of funny and weird requirement this time. I have my
This requirement is just for simplicity for developers and beautiful code. I'm building a
I just read this requirement on a job listing: Aware of the pitfalls of
Wondering if I could get some advice and direction on this following requirement: Need
the Requirement in simple words goes like this. Its a charting Application ( kinda

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.