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
PeriodicTaskwould 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.
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.