Thread Control in Java

<p>While the <i><b>suspend&lpar; &rpar;<&sol;b><&sol;i>&comma;<i><b> resume&lpar; &rpar;<&sol;b><&sol;i>&comma; and <b><i>stop&lpar; &rpar; <&sol;i><&sol;b>methods defined by <b><i>Thread <&sol;i><&sol;b>class seem to be a perfectly reasonable and convenient approach to managing the execution of threads&comma; they must not be used for new Java programs and obsolete in newer versions of Java&period;<&sol;p>&NewLine;<p>The following example illustrates how the<i><b> wait&lpar; &rpar; and notify&lpar; &rpar; <&sol;b><&sol;i>methods that are inherited from <i><b>Object <&sol;b><&sol;i>can be used to control the execution of a thread&period;<&sol;p>&NewLine;<p>This example is similar to the program in the previous section&period; However&comma; the deprecated method calls have been removed&period; Let us consider the operation of this program&period;<&sol;p>&NewLine;<p>The <i><b>NewThread <&sol;b><&sol;i>class contains a <b><i>boolean <&sol;i><&sol;b>instance variable named <b><i>suspendFlag<&sol;i><&sol;b>&comma; which is used to control the execution of the thread&period; It is initialized to false by the constructor&period;<&sol;p>&NewLine;<p>The <i><b>run&lpar; &rpar;<&sol;b><&sol;i> method contains a synchronized statement block that checks <b><i>suspendFlag<&sol;i><&sol;b>&period; If that variable is true&comma; the <i><b>wait&lpar; &rpar;<&sol;b><&sol;i> method is invoked to suspend the execution of the thread&period; The <b><i>mysuspend&lpar; &rpar; <&sol;i><&sol;b>method sets <i><b>suspendFlag <&sol;b><&sol;i>to true&period; The<i><b> myresume&lpar; &rpar;<&sol;b><&sol;i> method sets <i><b>suspendFlag <&sol;b><&sol;i>to false and invokes <b><i>notify&lpar; &rpar;<&sol;i><&sol;b> to wake up the thread&period; Finally&comma; the main&lpar; &rpar; method has been modified to invoke the <b><i>mysuspend&lpar; &rpar; and myresume&lpar; &rpar;<&sol;i><&sol;b> methods&period;<&sol;p>&NewLine;<p><b>Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&sol; Suspending and resuming a thread for Java 2 &NewLine;class NewThread implements Runnable &lbrace; &NewLine; String name&semi; &sol;&sol; name of thread &NewLine; Thread t&semi; &NewLine; boolean suspendFlag&semi; &NewLine; NewThread&lpar;String threadname&rpar; &lbrace; &NewLine; name &equals; threadname&semi; &NewLine; t &equals; new Thread&lpar;this&comma; name&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"New thread&colon; " &plus; t&rpar;&semi; &NewLine; suspendFlag &equals; false&semi; &NewLine; t&period;start&lpar;&rpar;&semi; &sol;&sol; Start the thread &NewLine; &rcub; &NewLine; &sol;&sol; This is the entry point for thread&period; &NewLine; public void run&lpar;&rpar; &lbrace; &NewLine; try &lbrace; &NewLine; for&lpar;int i &equals; 15&semi; i &gt&semi; 0&semi; i--&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;name &plus; "&colon; " &plus; i&rpar;&semi; &NewLine; Thread&period;sleep&lpar;200&rpar;&semi; &NewLine; synchronized&lpar;this&rpar; &lbrace; &NewLine; while&lpar;suspendFlag&rpar; &lbrace; &NewLine; wait&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;name &plus; " interrupted&period;"&rpar;&semi; &NewLine; &rcub; &NewLine; System&period;out&period;println&lpar;name &plus; " exiting&period;"&rpar;&semi; &NewLine; &rcub; &NewLine; void mysuspend&lpar;&rpar; &lbrace; &NewLine; suspendFlag &equals; true&semi; &NewLine; &rcub; &NewLine; synchronized void myresume&lpar;&rpar; &lbrace; &NewLine; suspendFlag &equals; false&semi; &NewLine; notify&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;public class SuspendResume &lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar; &lbrace; &NewLine; NewThread ob1 &equals; new NewThread&lpar;"One"&rpar;&semi; &NewLine; NewThread ob2 &equals; new NewThread&lpar;"Two"&rpar;&semi; &NewLine; try &lbrace; &NewLine; Thread&period;sleep&lpar;1000&rpar;&semi; &NewLine; ob1&period;mysuspend&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Suspending thread One"&rpar;&semi; &NewLine; Thread&period;sleep&lpar;1000&rpar;&semi; &NewLine; ob1&period;myresume&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Resuming thread One"&rpar;&semi; &NewLine; ob2&period;mysuspend&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Suspending thread Two"&rpar;&semi; &NewLine; Thread&period;sleep&lpar;1000&rpar;&semi; &NewLine; ob2&period;myresume&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Resuming thread Two"&rpar;&semi; &NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Main thread Interrupted"&rpar;&semi; &NewLine; &rcub; &NewLine; &sol;&sol; wait for threads to finish &NewLine; try &lbrace; &NewLine; System&period;out&period;println&lpar;"Waiting for threads to finish&period;"&rpar;&semi; &NewLine; ob1&period;t&period;join&lpar;&rpar;&semi; &NewLine; ob2&period;t&period;join&lpar;&rpar;&semi; &NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Main thread Interrupted"&rpar;&semi; &NewLine; &rcub; &NewLine; System&period;out&period;println&lpar;"Main thread exiting&period;"&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Here is the output produced by the above program&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;controlThread&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;inter-thread-communication-is-one-of&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;interrupting-java-threads&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;inter-thread-communication-is-one-of&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;interrupting-java-threads&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '472'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago