{"id":10210,"date":"2015-08-25T00:00:13","date_gmt":"2015-08-25T04:00:13","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10210"},"modified":"2015-08-24T08:41:42","modified_gmt":"2015-08-24T12:41:42","slug":"stoppablethread-java-a-template-to-place-a-thread-in-a-run-wait-or-stop-state","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10210","title":{"rendered":"StoppableThread.java A template to place a thread in a RUN, WAIT, or STOP state."},"content":{"rendered":"<pre>\/** A template to control the state of a thread through setting\r\n\u00a0*\u00a0 an internal flag.\r\n\r\npublic class StoppableThread extends Thread {\r\n\r\n\u00a0\u00a0 public static final int STOP\u00a0\u00a0\u00a0 = 0;\r\n\u00a0\u00a0 public static final int RUN\u00a0\u00a0\u00a0\u00a0 = 1;\r\n\u00a0\u00a0 public static final int WAIT\u00a0\u00a0\u00a0 = 2;\r\n\u00a0\u00a0 private int state = RUN;\r\n\r\n\u00a0 \/** Public method to permit setting a flag to stop or\r\n\u00a0\u00a0 *\u00a0 suspend the thread.\u00a0 The state is monitored through the\r\n\u00a0\u00a0 *\u00a0 corresponding checkState method.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0\u00a0 public synchronized void setState(int state) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 this.state = state;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (state==RUN) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 notify();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 }\r\n\r\n\u00a0 \/** Returns the desired state of the thread (RUN, STOP, WAIT).\r\n\u00a0\u00a0 *\u00a0 Normally, you may want to change the state or perform some\r\n\u00a0\u00a0 *\u00a0 other task if an InterruptedException occurs.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0\u00a0 private synchronized int checkState() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 while (state==WAIT) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 wait();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } catch (InterruptedException e) { }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return state;\r\n\u00a0\u00a0 }\r\n\r\n\u00a0 \/** An example of thread that will continue to run until\r\n\u00a0\u00a0 *\u00a0 the creating object tells the thread to STOP.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0\u00a0 public void run() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 while (checkState()!=STOP) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ...\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** A template to control the state of a thread through setting \u00a0*\u00a0 an internal flag. public class StoppableThread extends Thread { \u00a0\u00a0 public static final int STOP\u00a0\u00a0\u00a0 = 0; \u00a0\u00a0 public static final int RUN\u00a0\u00a0\u00a0\u00a0 = 1; \u00a0\u00a0 public static final int WAIT\u00a0\u00a0\u00a0 = 2; \u00a0\u00a0 private int state = RUN; \u00a0 \/** Public &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10210\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,285],"class_list":["post-10210","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26858,"url":"http:\/\/bangla.sitestree.com\/?p=26858","url_meta":{"origin":10210,"position":0},"title":"# StoppableThread.java A template to place a thread in a RUN, WAIT, or STOP state. #Programming Code Examples #Java\/J2EE\/J2ME #Java Threads","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"\/** A template to control the state of a thread through setting * an internal flag. public class StoppableThread extends Thread { public static final int STOP = 0; public static final int RUN = 1; public static final int WAIT = 2; private int state = RUN; \/** Public\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26860,"url":"http:\/\/bangla.sitestree.com\/?p=26860","url_meta":{"origin":10210,"position":1},"title":"Multithreaded Graphics and Double Buffering #Programming Code Examples #Java\/J2EE\/J2ME #Java Threads","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"ShipSimulation.java Illustrates the basic approach of multithreaded graphics whereas a thread adjusts parameters affecting the appearance of the graphics and then calls repaint to schedule an update of the display. import java.applet.Applet; import java.awt.*; public class ShipSimulation extends Applet implements Runnable { ... public void run() { Ship s; for(int\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10250,"url":"http:\/\/bangla.sitestree.com\/?p=10250","url_meta":{"origin":10210,"position":2},"title":"Creates and starts three threaded objects","author":"","date":"August 25, 2015","format":false,"excerpt":"Creates and starts three threaded objects which count from 0 to 4. Uses the following class: CounterTest.java Counter.java \/** Try out a few instances of the Counter class. public class CounterTest { public static void main(String[] args) { Counter c1 = new Counter(5); Counter c2 = new Counter(5); Counter c3\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10212,"url":"http:\/\/bangla.sitestree.com\/?p=10212","url_meta":{"origin":10210,"position":3},"title":"Multithreaded Graphics and Double Buffering","author":"","date":"August 25, 2015","format":false,"excerpt":"ShipSimulation.java\u00a0 Illustrates the basic approach of multithreaded graphics whereas a thread adjusts parameters affecting the appearance of the graphics and then calls repaint to schedule an update of the display. \u00a0 import java.applet.Applet; import java.awt.*; public class ShipSimulation extends Applet implements Runnable { \u00a0 ... \u00a0 \u00a0 public void run()\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10262,"url":"http:\/\/bangla.sitestree.com\/?p=10262","url_meta":{"origin":10210,"position":4},"title":"ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method.","author":"","date":"August 26, 2015","format":false,"excerpt":"ThreadedRSAKey.java\u00a0 Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method. In this example, RSAKey\u00a0 computes an RSA public-private key pair, where the key size has a specified number of digits. As large prime numbers require considerable CPU time, ThreadedRSAKey converts the original computeKey\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26547,"url":"http:\/\/bangla.sitestree.com\/?p=26547","url_meta":{"origin":10210,"position":5},"title":"PrimeNumbers.java  Servlet that processes a request to generate n prime numbers, each with at least m digits. If these results are not complete, it sends a Refresh header instructing the browser to ask for new results a little while later. Uses the Primes #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"PrimeNumbers.java Servlet that processes a request to generate n prime numbers, each with at least m digits. If these results are not complete, it sends a Refresh header instructing the browser to ask for new results a little while later. Uses the Primes, PrimeList, and ServletUtilities classes. package cwp; import\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10210"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10210\/revisions"}],"predecessor-version":[{"id":10211,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10210\/revisions\/10211"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10210"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}