<p>While the <i><b>suspend( )</b></i>,<i><b> resume( )</b></i>, and <b><i>stop( ) </i></b>methods defined by <b><i>Thread </i></b>class seem to be a perfectly reasonable and convenient approach to managing the execution of threads, they must not be used for new Java programs and obsolete in newer versions of Java.</p>
<p>The following example illustrates how the<i><b> wait( ) and notify( ) </b></i>methods that are inherited from <i><b>Object </b></i>can be used to control the execution of a thread.</p>
<p>This example is similar to the program in the previous section. However, the deprecated method calls have been removed. Let us consider the operation of this program.</p>
<p>The <i><b>NewThread </b></i>class contains a <b><i>boolean </i></b>instance variable named <b><i>suspendFlag</i></b>, which is used to control the execution of the thread. It is initialized to false by the constructor.</p>
<p>The <i><b>run( )</b></i> method contains a synchronized statement block that checks <b><i>suspendFlag</i></b>. If that variable is true, the <i><b>wait( )</b></i> method is invoked to suspend the execution of the thread. The <b><i>mysuspend( ) </i></b>method sets <i><b>suspendFlag </b></i>to true. The<i><b> myresume( )</b></i> method sets <i><b>suspendFlag </b></i>to false and invokes <b><i>notify( )</i></b> to wake up the thread. Finally, the main( ) method has been modified to invoke the <b><i>mysuspend( ) and myresume( )</i></b> methods.</p>
<p><b>Example:</b></p>
<pre class="highlight">// Suspending and resuming a thread for Java 2 
class NewThread implements Runnable { 
 String name; // name of thread 
 Thread t; 
 boolean suspendFlag; 
 NewThread(String threadname) { 
 name = threadname; 
 t = new Thread(this, name); 
 System.out.println("New thread: " + t); 
 suspendFlag = false; 
 t.start(); // Start the thread 
 } 
 // This is the entry point for thread. 
 public void run() { 
 try { 
 for(int i = 15; i >; 0; i--) { 
 System.out.println(name + ": " + i); 
 Thread.sleep(200); 
 synchronized(this) { 
 while(suspendFlag) { 
 wait(); 
 } 
 } 
 } 
 } catch (InterruptedException e) { 
 System.out.println(name + " interrupted."); 
 } 
 System.out.println(name + " exiting."); 
 } 
 void mysuspend() { 
 suspendFlag = true; 
 } 
 synchronized void myresume() { 
 suspendFlag = false; 
 notify(); 
 } 
} 
 
public class SuspendResume { 
 public static void main(String args[]) { 
 NewThread ob1 = new NewThread("One"); 
 NewThread ob2 = new NewThread("Two"); 
 try { 
 Thread.sleep(1000); 
 ob1.mysuspend(); 
 System.out.println("Suspending thread One"); 
 Thread.sleep(1000); 
 ob1.myresume(); 
 System.out.println("Resuming thread One"); 
 ob2.mysuspend(); 
 System.out.println("Suspending thread Two"); 
 Thread.sleep(1000); 
 ob2.myresume(); 
 System.out.println("Resuming thread Two"); 
 } catch (InterruptedException e) { 
 System.out.println("Main thread Interrupted"); 
 } 
 // wait for threads to finish 
 try { 
 System.out.println("Waiting for threads to finish."); 
 ob1.t.join(); 
 ob2.t.join(); 
 } catch (InterruptedException e) { 
 System.out.println("Main thread Interrupted"); 
 } 
 System.out.println("Main thread exiting."); 
 } 
} 
</pre>
<p><b>Here is the output produced by the above program:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/controlThread.png" border="0" /></div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/inter-thread-communication-is-one-of/">Previous</a> <;<; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>|| >;>;<a href="https://dineshonjava.com/interrupting-java-threads/">Next</a> >;>;</b></div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/inter-thread-communication-is-one-of/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/interrupting-java-threads/">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: '472'});
});
</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…