Nasif Imtiaz Ohi
Nasif Imtiaz Ohi

Reputation: 1713

How to use java Thread.currentThread().interrupt()

if I override my run function as ,

Thread t = new Thread(){
                public void run(){
                    try {
                        if(Thread.currentThread().isInterrupted()) {
                            doSomePrcocess() // is the isInerrupted() flag seeting to true?
                            return; //Terminates the current Thread
                        }
                        //otherwise 
                        runScript();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();

and then If I call, Thread.currentThread().interrupt() from any point in the code, should the thread halt there and start running doSomeProcess() at that point? if yes, then how the interrupted flag gets to set true? If no, how to do this?

Upvotes: 0

Views: 2856

Answers (3)

Javadee
Javadee

Reputation: 169

I have got a running example with comments in your code below. Try running it a few times to see if it clarifies your concept.

Normally you would interrupt a thread from another thread and yes doSomeProcess will get invoked in the next cycle of the loop which could be 1 ms after the thread was interrupted or 1 hour after depending on the logic implemented in your methods.

public class InterruptTest {

    public static void main(String[] args) throws InterruptedException {

        Thread t = new Thread() {
            public void run() {
                while (true) {
                    try {

                        if (Thread.currentThread().isInterrupted()) {
                            doSomePrcocess(); // is the isInerrupted() flag seeting to true? - Yes
                            return; // Terminates the current Thread - yes
                        }
                        // otherwise
                        runScript();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            private void runScript() {
                System.out.println("runScript interrupted status:" + this.isInterrupted());
                sleepy(100);


            }

            private void doSomePrcocess() {
                System.out.println("doSomePrcocess interrupted status:" + this.isInterrupted());
                sleepy(500);
            }

            private void sleepy(int millis) {
                try {
                    Thread.sleep(millis);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Thread.currentThread().interrupt(); // try commenting this out to see what happens.
                }
            }
        };
        t.start(); 

        Thread.sleep(1000);

        t.interrupt(); // generally you would call interrupt on another thread.

    }

}

Upvotes: 1

Rizwan
Rizwan

Reputation: 2437

  • If thread is in sleeping or waiting state calling the interrupt() method on the thread, breaks out the sleeping or waiting state

throwing InterruptedException

  • If the thread is not in the sleeping or waiting state, calling the interrupt() method performs normal behaviour and doesn't interrupt the thread but sets the interrupt flag to true.

Thread class has provision to deal with thread interruption as

public void interrupt()
public static boolean interrupted()
public boolean isInterrupted()

If you intend to go with the only once execution of doSomePrcocess then you have to go with which will check and clear the Thread interruption state for successive calls.

public static boolean interrupted()

Using below will only check the status and no modification.

public boolean isInterrupted()

Upvotes: 2

ewramner
ewramner

Reputation: 6233

No, it doesn't work like that. The isInterrupted method checks if the flag is set, it does not declare a handler. There is no way to define a central handler that will automatically be called when a thread is interrupted. What you can do is to catch InterruptedException and call the handler, plus check the interrupt flag regularly to see if it is time to stop.

Upvotes: 0

Related Questions