<p>Interrupting a thread means stopping what it is doing before it has completed its task, effectively aborting its current operation. Whether the thread dies, waits for new tasks, or goes on to the next step depends on the application.</p>
<p> If any thread is in sleeping or waiting state (<i><b>i.e. sleep() or wait() is invoked</b></i>), calling the<b><i> interrupt()</i></b> method on the thread, breaks out the sleeping or waiting state throwing <b><i>InterruptedException</i></b>. If the thread is not in the sleeping or waiting state, calling the <i><b>interrupt()</b></i> method performs normal behavior and doesn&#8217;t interrupt the thread but sets the interrupt flag to true. Let&#8217;s first see the methods provided by the Thread class for thread interruption.</p>
<p> <b>The 3 methods provided by the <i>Thread </i>class for interrupting a thread</b></p>
<ul>
<li><i><b>public void interrupt()</b></i></li>
<li><i><b>public static boolean interrupted()</b></i></li>
<li><i><b>public boolean isInterrupted()</b></i></li>
</ul>
<p><b>Example of interrupting a thread that stops working</b></p>
<pre class="highlight" name="code">class InterruptDemo extends Thread{ 
 public void run(){ 
 try{ 
 Thread.sleep(1000); 
 System.out.println("task"); 
 }catch(InterruptedException e){ 
 throw new RuntimeException("Thread interrupted..."+e); 
 } 
 
} 
 
public static void main(String args[]){ 
 InterruptDemo t1 = new InterruptDemo(); 
 t1.start(); 
 try{ 
 t1.interrupt(); 
 }catch(Exception e){ 
 System.out.println("Exception handled "+e); 
 } 
 
 } 
} 
</pre>
<p><b><br />
output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/interrupt.png" /></div>
<p>
<b>Example of interrupting a thread that doesn&#8217;t stop working</b></p>
<pre class="highlight" name="code">class InterruptDemo extends Thread{ 
 public void run(){ 
 try{ 
 Thread.sleep(1000); 
 System.out.println("task"); 
 }catch(InterruptedException e){ 
 System.out.println("Exception handled "+e); 
 } 
 
} 
 
public static void main(String args[]){ 
 InterruptDemo t1 = new InterruptDemo(); 
 t1.start(); 
 try{ 
 t1.interrupt(); 
 }catch(Exception e){ 
 System.out.println("Exception handled "+e); 
 } 
 System.out.println("thread is running..."); 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/interrupt2.png" /></div>
<p><b>Example of interrupting thread that behaves normally</b><br />
If thread is not in sleeping or waiting state, calling the <b><i>interrupt()</i></b> method sets the interrupted flag to true that can be used to stop the thread by the java programmer later.</p>
<pre class="highlight" name="code">class InterruptThread extends Thread{ 
 
 public void run(){ 
 for(int i=1;i<;=5;i++) 
 System.out.println(i); 
 } 
 
 public static void main(String args[]){ 
 InterruptThread t1 = new InterruptThread(); 
 t1.start(); 
 
 t1.interrupt(); 
 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/interrupt3.png" /></div>
<p><b>What about <i>isInterrupted </i>and <i>interrupted </i>method?</b><br />
The <i><b>isInterrupted()</b></i> method returns the interrupted flag either true or false. The static interrupted() method returns the interrupted flag after that it sets the flag to false if it is true.</p>
<pre class="highlight" name="code">class InterruptedDemo extends Thread{ 
 
 public void run(){ 
 for(int i=1;i<;=2;i++){ 
 if(Thread.interrupted()){ 
 System.out.println("code for interrupted thread"); 
 } 
 else{ 
 System.out.println("code for normal thread"); 
 } 
 
 }//end of for loop 
 } 
 
 public static void main(String args[]){ 
 
 InterruptedDemo t1=new InterruptedDemo(); 
 InterruptedDemo t2=new InterruptedDemo(); 
 
 t1.start(); 
 t1.interrupt(); 
 
 t2.start(); 
 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/interrupt4.png" /></div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/thread-control-in-java_9075/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/difference-between-wait-and-sleep/">Next</a> >;>;</b></div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/thread-control-in-java_9075/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/difference-between-wait-and-sleep/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '471'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…