{"id":10254,"date":"2015-08-25T07:34:01","date_gmt":"2015-08-25T11:34:01","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10254"},"modified":"2015-08-24T08:45:44","modified_gmt":"2015-08-24T12:45:44","slug":"counter2test-java-driver-class-that-creates-three-threaded-objects-counter2-that-count-from-0-to-4","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10254","title":{"rendered":"Counter2Test.java Driver class that creates three threaded objects (Counter2) that count from 0 to 4."},"content":{"rendered":"<pre>\/** Try out a few instances of the Counter2 class. \r\n\r\nDriver class that creates three threaded objects (Counter2) that count from 0 to 4. In this case, \r\nthe driver does not start the threads, as each thread is automatically started in Counter2's \r\nconstructor. Uses the following class:\r\n\r\nCounter2Test.java\r\nCounter2.java\r\n**************************\u00a0\u00a0 \u00a0\r\npublic class Counter2Test {\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 Counter2 c1 = new Counter2(5);\r\n\u00a0\u00a0\u00a0 Counter2 c2 = new Counter2(5);\r\n\u00a0\u00a0\u00a0 Counter2 c3 = new Counter2(5);\r\n\u00a0 }\r\n}\r\n\r\n* Counter2.java A Runnable\u00a0 that counts to a specified value and sleeps a random number of seconds \r\nin between counts. Here, the thread is started automatically when the object is instantiated.\r\n\/** A Runnable that counts up to a specified\r\n\u00a0*\u00a0 limit with random pauses in between each count.\r\n\u00a0\r\npublic class Counter2 implements Runnable {\r\n\u00a0 private static int totalNum = 0;\r\n\u00a0 private int currentNum, loopLimit;\r\n\r\n\u00a0 public Counter2(int loopLimit) {\r\n\u00a0\u00a0\u00a0 this.loopLimit = loopLimit;\r\n\u00a0\u00a0\u00a0 currentNum = totalNum++;\r\n\u00a0\u00a0\u00a0 Thread t = new Thread(this);\r\n\u00a0\u00a0\u00a0 t.start();\r\n\u00a0 }\r\n\r\n\u00a0 private void pause(double seconds) {\r\n\u00a0\u00a0\u00a0 try { Thread.sleep(Math.round(1000.0*seconds)); }\r\n\u00a0\u00a0\u00a0 catch(InterruptedException ie) {}\r\n\u00a0 }\r\n\u00a0 \r\n\u00a0 public void run() {\r\n\u00a0\u00a0\u00a0 for(int i=0; i<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** 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&#8217;s constructor. Uses the following class: Counter2Test.java Counter2.java **************************\u00a0\u00a0 \u00a0 public class Counter2Test { &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10254\">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-10254","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":27180,"url":"http:\/\/bangla.sitestree.com\/?p=27180","url_meta":{"origin":10254,"position":0},"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":10256,"url":"http:\/\/bangla.sitestree.com\/?p=10256","url_meta":{"origin":10254,"position":1},"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":27182,"url":"http:\/\/bangla.sitestree.com\/?p=27182","url_meta":{"origin":10254,"position":2},"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":10259,"url":"http:\/\/bangla.sitestree.com\/?p=10259","url_meta":{"origin":10254,"position":3},"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":[]},{"id":27184,"url":"http:\/\/bangla.sitestree.com\/?p=27184","url_meta":{"origin":10254,"position":4},"title":"Buggy Counter Applet.java Demonstrates that data shared by multiple threads is candidate for a potential race condition #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","format":false,"excerpt":"import java.applet.Applet; import java.awt.*; \/** Emulates the Counter and Counter2 classes, but this time * from an applet that invokes multiple versions of its own run * method. This version is likely to work correctly * except when an important customer is visiting. public class BuggyCounterApplet extends Applet implements Runnable{\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":10254,"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\/10254","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=10254"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10254\/revisions"}],"predecessor-version":[{"id":10255,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10254\/revisions\/10255"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10254"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}