{"id":27176,"date":"2021-05-13T23:10:05","date_gmt":"2021-05-14T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/creates-and-starts-three-threaded-objects-programming-code-examples-java-j2ee-j2me-advanced-swing\/"},"modified":"2021-05-13T23:10:05","modified_gmt":"2021-05-14T03:10:05","slug":"creates-and-starts-three-threaded-objects-programming-code-examples-java-j2ee-j2me-advanced-swing","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27176","title":{"rendered":"Creates and starts three threaded objects #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing"},"content":{"rendered":"<pre>\nCreates and starts three threaded objects which count from 0 to 4. Uses the following class:\nCounterTest.java\nCounter.java\n\/** Try out a few instances of the Counter class. \n public class CounterTest {\n  public static void main(String[] args) {\n    Counter c1 = new Counter(5);\n    Counter c2 = new Counter(5);\n    Counter c3 = new Counter(5);\n    c1.start();\n    c2.start();\n    c3.start();\n  }\n}\n\nCounter.java:A class that inherits from Thread and defines a run method that counts up to a specified value, pausing for a random time interval in between value counts.\n\/** A subclass of Thread that counts up to a specified\n *  limit with random pauses in between each count.\n \npublic class Counter extends Thread {\n  private static int totalNum = 0;\n  private int currentNum, loopLimit;\n\n  public Counter(int loopLimit) {\n    this.loopLimit = loopLimit;\n    currentNum = totalNum++;\n  }\n\n  private void pause(double seconds) {\n    try { Thread.sleep(Math.round(1000.0*seconds)); }\n    catch(InterruptedException ie) {}\n  }\n\n  \/** When run finishes, the thread exits. *\/\n  \n  public void run() {\n    for(int i=0; i&lt;looplimit ; i++) {\n      System.out.println(&quot;Counter &quot; + currentNum + &quot;: &quot; + i);\n      pause(Math.random()); \/\/ Sleep for up to 1 second\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=10291<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Advanced Swing<br \/>Tags:Java\/J2EE\/J2MEAdvanced Swing<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>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 = new Counter(5); c1.start(); c2.start(); &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=27176\">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-27176","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":10250,"url":"http:\/\/bangla.sitestree.com\/?p=10250","url_meta":{"origin":27176,"position":0},"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":27180,"url":"http:\/\/bangla.sitestree.com\/?p=27180","url_meta":{"origin":27176,"position":1},"title":"Counter2Test.java Driver class that creates three threaded objects (Counter2) that count from 0 to 4. #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","format":false,"excerpt":"\/** Try out a few instances of the Counter2 class. Driver class that creates three threaded objects (Counter2) that count from 0 to 4. In this case, the driver does not start the threads, as each thread is automatically started in Counter2's constructor. Uses the following class: Counter2Test.java Counter2.java **************************\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":10254,"url":"http:\/\/bangla.sitestree.com\/?p=10254","url_meta":{"origin":27176,"position":2},"title":"Counter2Test.java Driver class that creates three threaded objects (Counter2) that count from 0 to 4.","author":"","date":"August 25, 2015","format":false,"excerpt":"\/** Try out a few instances of the Counter2 class. Driver class that creates three threaded objects (Counter2) that count from 0 to 4. In this case, the driver does not start the threads, as each thread is automatically started in Counter2's constructor. Uses the following class: Counter2Test.java Counter2.java **************************\u00a0\u00a0\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":27182,"url":"http:\/\/bangla.sitestree.com\/?p=27182","url_meta":{"origin":27176,"position":3},"title":"Driver class that creates three threaded objects (Counter2) that count from 0 to 4. #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","format":false,"excerpt":"\/** Try out a few instances of the Counter2 class. public class Counter2Test { public static void main(String[] args) { Counter2 c1 = new Counter2(5); Counter2 c2 = new Counter2(5); Counter2 c3 = new Counter2(5); } } \/** A Runnable that counts up to a specified * limit with random\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":10256,"url":"http:\/\/bangla.sitestree.com\/?p=10256","url_meta":{"origin":27176,"position":4},"title":"Driver class that creates three threaded objects (Counter2) that count from 0 to 4.","author":"","date":"August 25, 2015","format":false,"excerpt":"\/** Try out a few instances of the Counter2 class. public class Counter2Test { \u00a0 public static void main(String[] args) { \u00a0\u00a0\u00a0 Counter2 c1 = new Counter2(5); \u00a0\u00a0\u00a0 Counter2 c2 = new Counter2(5); \u00a0\u00a0\u00a0 Counter2 c3 = new Counter2(5); \u00a0 } } \/** A Runnable that counts up to a\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":10259,"url":"http:\/\/bangla.sitestree.com\/?p=10259","url_meta":{"origin":27176,"position":5},"title":"Buggy Counter Applet.java Demonstrates that data shared by multiple threads is candidate for a potential race condition","author":"","date":"August 23, 2015","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; \/** Emulates the Counter and Counter2 classes, but this time \u00a0*\u00a0 from an applet that invokes multiple versions of its own run \u00a0*\u00a0 method. This version is likely to work correctly \u00a0*\u00a0 except when\u00a0 an important customer is visiting. public class BuggyCounterApplet extends Applet \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements\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\/27176","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=27176"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27176\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27176"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}