{"id":26858,"date":"2021-05-03T23:10:05","date_gmt":"2021-05-04T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/stoppablethread-java-a-template-to-place-a-thread-in-a-run-wait-or-stop-state-programming-code-examples-java-j2ee-j2me-java-threads\/"},"modified":"2021-05-03T23:10:05","modified_gmt":"2021-05-04T03:10:05","slug":"stoppablethread-java-a-template-to-place-a-thread-in-a-run-wait-or-stop-state-programming-code-examples-java-j2ee-j2me-java-threads","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26858","title":{"rendered":"# StoppableThread.java A template to place a thread in a RUN, WAIT, or STOP state. #Programming Code Examples #Java\/J2EE\/J2ME #Java Threads"},"content":{"rendered":"<pre>\n\n\/** A template to control the state of a thread through setting\n *  an internal flag.\n\npublic class StoppableThread extends Thread {\n\n   public static final int STOP    = 0;\n   public static final int RUN     = 1;\n   public static final int WAIT    = 2;\n   private int state = RUN;\n\n  \/** Public method to permit setting a flag to stop or\n   *  suspend the thread.  The state is monitored through the\n   *  corresponding checkState method.\n   *\/\n\n   public synchronized void setState(int state) {\n      this.state = state;\n      if (state==RUN) {\n         notify();\n      }\n   }\n\n  \/** Returns the desired state of the thread (RUN, STOP, WAIT).\n   *  Normally, you may want to change the state or perform some\n   *  other task if an InterruptedException occurs.\n   *\/\n\n   private synchronized int checkState() {\n      while (state==WAIT) {\n        try {\n          wait();\n        } catch (InterruptedException e) { }\n      }\n      return state;\n   }\n\n  \/** An example of thread that will continue to run until\n   *  the creating object tells the thread to STOP.\n   *\/\n\n   public void run() {\n      while (checkState()!=STOP) {\n         ...\n      }\n   }\n}\n\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10297<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Java Threads<br \/>Tags:Java\/J2EE\/J2MEJava Threads<br \/> Post Data:2017-01-02 16:04:31<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/** 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 method to permit setting a &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26858\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26858","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10210,"url":"http:\/\/bangla.sitestree.com\/?p=10210","url_meta":{"origin":26858,"position":0},"title":"StoppableThread.java A template to place a thread in a RUN, WAIT, or STOP state.","author":"","date":"August 25, 2015","format":false,"excerpt":"\/** 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\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":26860,"url":"http:\/\/bangla.sitestree.com\/?p=26860","url_meta":{"origin":26858,"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":26547,"url":"http:\/\/bangla.sitestree.com\/?p=26547","url_meta":{"origin":26858,"position":2},"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":[]},{"id":78331,"url":"http:\/\/bangla.sitestree.com\/?p=78331","url_meta":{"origin":26858,"position":3},"title":"Demonstrating wait() and notify() in a simple producer-consumer problem&#8221;","author":"Sayed","date":"July 27, 2025","format":false,"excerpt":"Check on the methods on: https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Object.html Understanding wait(), notify(), and notifyAll() in Java In Java and Java EE, the methods wait(), notify(), and notifyAll() are defined in the java.lang.Object class. They are not part of classes like Thread or interfaces such as Runnable. How They Work: These methods are part\u2026","rel":"","context":"In &quot;Anything JAVA&quot;","block_context":{"text":"Anything JAVA","link":"http:\/\/bangla.sitestree.com\/?cat=1975"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10173,"url":"http:\/\/bangla.sitestree.com\/?p=10173","url_meta":{"origin":26858,"position":4},"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","author":"","date":"August 18, 2015","format":false,"excerpt":"PrimeNumbers.java\u00a0 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\u00a0 classes. \u00a0 package cwp;\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":10250,"url":"http:\/\/bangla.sitestree.com\/?p=10250","url_meta":{"origin":26858,"position":5},"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":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26858","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\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26858"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26858\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26858"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}