Awakening of a sleeping thread - interruption () in comparison with "splitting" of sleep into several berths

This requirement appeared in my Android application, but it applies to Java in general. My application does something every few seconds. I implemented this as follows (only the relevant snippets are not 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 the completion of the previous dream ().

Since my use case allows the end user to set the interval in fixed steps (1 second - from 1 to 60 seconds), I modified the implementation for sleep in a loop; and check the value of the new interval 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;
            }
        }
    }
}

?

interrupt() . , . , , , static. , Thread ?

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

-, Thread, , catch InterruptedException . , startTask() . . SO (), , .

?


EDIT- :

MY app , REST- . .

, , 60 . Snippet1, , () :

  • 60 .
  • , , 5 . .
  • PeriodicTask 60 .

, (, , , 1 ), , ).

My Snippet2 Snippet3 .

+5
4

IIRC, Java object.wait() . , ? - , waitValue notify(). "" , . .

+9

. , . startTask() setInterval().

public class PeriodicTask {

    private volatile boolean running = true;
    private volatile int interval = 5;
    private final Object lockObj = new Object();

    public void startTask() {
        while (running) {
            doSomething();
            synchronized (lockObj) {
                try{
                    lockObj.wait(interval * 1000);
                } catch(InterruptedException e){
                    //Handle Exception
                }
            }
        }
    }

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

    public void setInterval(int newInterval) {
        synchronized (lockObj) {
            this.interval = newInterval;
            lockObj.notify();
        }
    }
}
+6

, . , PeriodicTask, ? , , :

public class ThreadStopExample {

    public static void main ( String[] args ) throws InterruptedException {
        final PeriodicTask task = new PeriodicTask ();
        Thread t = new Thread ( new Runnable () {
            @Override
            public void run () {
                System.out.println ( Thread.currentThread ().getName () 
                    + " starting" );
                task.startTask ();
                System.out.println ( Thread.currentThread ().getName ()
                    + " done with the periodic task" );
            }
        } );
        t.start ();
        Thread.sleep ( 12000 );
        task.setInterval ( 1 );
        Thread.sleep ( 3000 );
        task.stopTask ();
    }

    static class PeriodicTask {

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

        public void startTask () {
            running = true;
            while ( running ) {
                doSomething ();
                try {
                    int count = 0;
                    while ( running && count++ < interval ) {
                        Thread.sleep ( 1000 );
                    }
                } catch ( InterruptedException e ) {
                    Thread.currentThread ().interrupt ();
                    running = false;
                    break;
                }
            }
        }

        public void stopTask () {
            running = false;
        }

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

        private void doSomething () {
            System.out.println ( "[" + Thread.currentThread ().getName () 
                + "] Interval: " + interval );
        }
    }
}

. , , PeriodicTask, , (. java). , , PeriodicTask, , . , PeriodicTask , . , , , . , PeriodicTask, - .

, , , PeriodicTask Thread, , , PeriodicTask Runnable. :

public class ThreadStopExample2 {

    public static void main ( String[] args ) throws InterruptedException {
        final PeriodicTask task = new PeriodicTask ();
        Thread t = new Thread ( task );
        t.start ();
        Thread.sleep ( 12000 );
        task.setInterval ( 1 );
        Thread.sleep ( 3000 );
        t.interrupt ();
    }

    static class PeriodicTask implements Runnable {

        private volatile int interval = 5;

        @Override
        public void run () {
            while ( true ) {
                doSomething ();
                try {
                    int count = 0;
                    while ( count++ < interval ) {
                        Thread.sleep ( 1000 );
                    }
                } catch ( InterruptedException e ) {
                    Thread.currentThread ().interrupt ();
                    break;
                }
            }
        }

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

        private void doSomething () {
            System.out.println ( "[" + Thread.currentThread ().getName () 
               + "] Interval: " + interval );
        }
    }
}

PeriodicTask , , , . PeriodicTask , , run.

: , PeriodicTask , , , . , PeriodicTask ( , ), , . , .

+4

interrupt() , , InterruptedException, catch. , . InterruptedException, .

, , , . , , , .

http://docs.oracle.com/javase/tutorial/essential/concurrency/simple.html http://docs.oracle.com/javase/tutorial/essential/ concurrency/interrupt.html

+1

All Articles