How a thread can interrupt an another thread in Java?

interrupt() method : If any thread is in sleeping or waiting state then using interrupt() method, we can interrupt the execution of that thread by showing InterruptedException. A thread which is in the sleeping or waiting state can be interrupted with the help of interrupt() method of Thread class.

Example : Suppose there are two threads and If one of the threads is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException, which gives the chance to another thread to execute the corresponding run() method of another thread which results into high performance and reduces the waiting time of the threads.

Different scenarios where we can interrupt a thread



  1. Interrupting a thread that doesn’t stop working : In the program, we handle the InterruptedException using try and catch block, so whenever any thread interrupt currently executing thread it will comes out from the sleeping state but it will not stop working.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to illustrate the
    // concept of interrupt() method
    // while a thread does not stops working
    class MyClass extends Thread {
        public void run()
        {
            try {
                for (int i = 0; i < 5; i++) {
                    System.out.println("Child Thread executing");
      
                    // Here current threads goes to sleeping state
                    // Another thread gets the chance to execute
                    Thread.sleep(1000);
                }
            }
            catch (InterruptedException e) {
                System.out.println("InterruptedException occur");
            }
        }
      
    class Test {
        public static void main(String[] args) 
                  throws InterruptedException
        {
            MyClass thread = new MyClass();
            thread.start();
      
            // main thread calls interrupt() method on 
            // child thread
            thread.interrupt();
      
            System.out.println("Main thread execution completes");
        }
    }

    chevron_right

    
    

    Output:

    Main thread execution completes
    Child Thread executing
    InterruptedException occur
    
  2. Interrupting a thread that stops working : In the program, after interrupting currently executing thread, we are throwing a new exception in the catch block so it will stop working.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to illustrate the
    // concept of interrupt() method
    // while a thread stops working
    class Geeks extends Thread {
        public void run()
        {
            try {
                Thread.sleep(2000);
                System.out.println("Geeksforgeeks");
            }
            catch (InterruptedException e) {
                throw new RuntimeException("Thread "
                                          "interrupted");
            }
        }
        public static void main(String args[])
        {
            Geeks t1 = new Geeks();
            t1.start();
            try {
                t1.interrupt();
            }
            catch (Exception e) {
                System.out.println("Exception handled");
            }
        }
    }

    chevron_right

    
    

    Output:

    Exception in thread "Thread-0" java.lang.RuntimeException: Thread interrupted
    
  3. Interrupting a thread that works normally : In the program, there is no exception occurred during the execution of thread. Here, interrupt only sets the interrupted flag to true, which can be used by java programmer later.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to illustrate the concept of
    // interrupt() method
    class Geeks extends Thread {
        public void run()
        {
            for (int i = 0; i < 5; i++)
                System.out.println(i);
        }
        public static void main(String args[])
        {
            Geeks t1 = new Geeks();
            t1.start();
            t1.interrupt();
        }
    }

    chevron_right

    
    

    Output:

    0
    1
    2
    3
    4
    

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.