
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How a thread can interrupt another thread in Java?
The ‘interrupt’ function can be used in Java to interrupt the execution of a thread with the help of the exception InterruptedException.
The below example shows how the currently executing thread stops execution (because of a new exception raised in the catch block) once it is interrupted −
Example
public class Demo extends Thread { public void run() { try { Thread.sleep(150); System.out.println("In the 'run' function inside try block"); } catch (InterruptedException e) { throw new RuntimeException("The thread has been interrupted"); } } public static void main(String args[]) { Demo my_inst = new Demo(); System.out.println("An instance of the Demo class has been created"); my_inst.start(); try { my_inst.interrupt(); } catch (Exception e) { System.out.println("The exception has been handled"); } } }
Output
An instance of the Demo class has been created Exception in thread "Thread-0" java.lang.RuntimeException: The thread has been interrupted at Demo.run(Demo.java:12)
A class named Demo extends the Thread class. Here, inside the ‘try’ block, a function named ‘run’ is defined that makes the function sleep for 150 milliseconds. In the ‘catch’ block, the exception is caught and relevant message is displayed on the console.
In the main function, an instance of the Demo class is created, and the thread is started using the ‘start’ function. Inside the ‘try’ block, the instance is interrupted, and in the ‘catch’ block, relevant message indicating the exception is printed.
- Related Articles
- How to interrupt a running thread in Java?
- How can we stop a thread in Java?
- How can we implement a timer thread in Java?
- How can dead thread be restarted in Java?
- User Thread vs Daemon Thread in Java?
- How to create a thread in Java
- Naming a thread in Java
- Thread Pools in Java
- Daemon thread in Java
- How to get the thread ID from a thread in C#?
- Inter thread communication in Java
- Demonstrate thread priorities in Java
- Change Thread Priority in Java
- Get current thread in Java
- Java Thread Priority in Multithreading
