Home » Java programming language

Java Thread Class void interrupt() method with Example

Java Thread Class void interrupt() method: Here, we are going to learn about the void interrupt() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 24, 2019

Thread Class void interrupt()

  • This method is available in package java.lang.Thread.interrupt().
  • This method is used to interrupt the thread.
  • We will discuss three cases
    1. If a thread is in sleeping or waiting state so in that case, we can interrupt the thread execution by throwing an exception(InterruptedException).
    2. If a thread is in sleeping or waiting state so, in that case, we can also interrupt the thread for normal execution by calling interrupt() method.
    3. If a thread is not in sleeping or waiting state so, in that case, we can also interrupt the thread for normal execution by calling interrupt() method.
  • This method is not static so we cannot access this method with the class name too.
  • The return type of this method is void so it does not return anything.
  • This method raises an exception if the current thread is not able to modify the thread.

Syntax:

    void interrupt(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is void, it returns nothing.

Java program to demonstrate example of interrupt() method

Case 1: We are interrupting the thread for stop execution of the thread if the thread is in sleeping mode.

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class InterruptedThread extends Thread  
{    
	// Overrides run()  method of Thread class
	public void run() 
	{   
		/*  The thread named InterruptedThread is in sleep mode 
			by using sleep(t) so this thread is giving a chance 
			for execution to another thread named "main"
		*/
		try{
			Thread.sleep(1000);    
			System.out.println("We are waiting till another thread execution");
		}catch(Exception ex){    
			System.out.println("InterruptedThread is interrupted "+ex.getMessage());
		}
	}
	public static void main(String args[])  
	{    
		InterruptedThread it = new InterruptedThread();    

		/*  By using start() method to call the run() method of 
			Thread class and Thread class start() will call run() 
			method of InterruptedThread class
		*/
		it.start(); 

		/*  Here thread named InterruptedThread got interrupted by
			using the interrupt() method 
		*/
		try  
		{    
			it.interrupt();    
		}catch(Exception ex){
			System.out.println("Exception handled "+ex.getMessage());
		}
	}
}    

Output

E:\Programs>javac InterruptedThread.java

E:\Programs>java InterruptedThread
Exception in thread "Thread-0" java.lang.RuntimeException: 
InterruptedThread is Interruptedsleep interrupted
        at InterruptedThread.run(InterruptedThread.java:13)

Case 2: We are interrupting the thread for normal execution without stopping if the thread is in sleeping mode.

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class InterruptedThread extends Thread  
{    
	// Overrides run()  method of Thread class
	public void run() 
	{

		/*  The thread named InterruptedThread is in sleep mode 
			by using sleep(t) so this thread is giving a chance 
			for execution to another thread named "main"
		*/
		try{
			Thread.sleep(1000);    
			System.out.println("We are waiting till another thread execution");
		}catch(Exception ex){
			System.out.println("Exception handled : "+ex.getMessage()); 
		}
		System.out.println("Interrupted thread is executing normally");
	}    
	public static void main(String args[])  
	{    
		InterruptedThread it = new InterruptedThread();    

		/*  By using start() method to call the run() method of 
			Thread class and Thread class start() will call run() 
			method of InterruptedThread class
		*/
		it.start(); 

		/*  Here thread named InterruptedThread got interrupted by
			using the interrupt() method 
		*/
		it.interrupt();    

	}    
}

Output

E:\Programs>javac InterruptedThread.java

E:\Programs>java InterruptedThread
Exception handled : sleep interrupted
Interrupted thread is executing normally

Case 3: We are interrupting the thread for normal execution if the thread is not in sleeping mode.

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class InterruptedThread extends Thread  
{    
	// Overrides run()  method of Thread class
	public void run() 
	{    
		System.out.println("We are executing this thread normally");
	}    
	public static void main(String args[])  
	{    
		InterruptedThread it = new InterruptedThread();    

		/*  By using start() method to call the run() method of 
			Thread class and Thread class start() will call run() 
			method of InterruptedThread class
		*/
		it.start(); 

		/*  Here thread named InterruptedThread got interrupted by 
			using the interrupt() method 
		*/
		it.interrupt();
	}    
} 

Output

E:\Programs>javac InterruptedThread.java
E:\Programs>java InterruptedThread
We are executing this thread normally


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.