Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waking up a sleeping thread - interrupt() versus "splitting" the sleep into multiple sleeps

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.

like image 384
curioustechizen Avatar asked Jan 03 '12 05:01

curioustechizen


People also ask

How do you wake up a sleeping thread?

Waking up Wait and Sleep We can wake the thread by calling either the notify() or notifyAll() methods on the monitor that is being waited on. Use notifyAll() instead of notify() when you want to wake all threads that are in the waiting state.

What is the difference between sleep () and wait () methods in Java multithreading?

Difference between wait() and sleep() The major difference is that wait() releases the lock while sleep() doesn't release any lock while waiting. wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution, generally.

What is the difference between thread and sleep?

Sleep() method belongs to Thread class. Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context.

Why thread sleep is not recommended?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

What happens when thread's sleep () method is called?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.


1 Answers

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.

like image 81
Martin James Avatar answered Oct 16 '22 16:10

Martin James