{"id":27186,"date":"2021-05-13T23:10:06","date_gmt":"2021-05-14T03:10:06","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/threadedrsakey-java-illustrates-converting-a-method-in-an-existing-class-from-a-single-threaded-method-to-a-multi-threaded-method-programming-code-examples-java-j2ee-j2me-advanced-swing\/"},"modified":"2021-05-13T23:10:06","modified_gmt":"2021-05-14T03:10:06","slug":"threadedrsakey-java-illustrates-converting-a-method-in-an-existing-class-from-a-single-threaded-method-to-a-multi-threaded-method-programming-code-examples-java-j2ee-j2me-advanced-swing","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27186","title":{"rendered":"ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method. #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing"},"content":{"rendered":"<pre>\nThreadedRSAKey.java  Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method. In this example, RSAKey  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 method in RSAKey  to a multi-threaded method, thus allowing simultaneous (multithreaded) computation of multiple key pairs. Uses the following classes:\nimport java.io.*;   \n\n * RSAKey.java Computes RSA public-private key pairs of arbitrary length.\n\n\n * Primes.java Generates large prime numbers.\n\n\n\/** An example of creating a background process for an\n *  originally nonthreaded, class method. Normally,\n *  the program flow will wait until computeKey is finished.\n \npublic class ThreadedRSAKey extends RSAKey implements Runnable {\n\n  \/\/ Store strNumDigits into the thread to prevent race\n  \/\/ conditions.\n  public void computeKey(String strNumDigits) {\n    RSAThread t = new RSAThread(this, strNumDigits);\n    t.start();\n  }\n\n  \/\/ Retrieve the stored strNumDigits and call the original\n  \/\/ method.  Processing is now done in the background.\n  public void run() {\n    RSAThread t = (RSAThread)Thread.currentThread();\n    String strNumDigits = t.getStrDigits();\n    super.computeKey(strNumDigits);\n  }\n\n  public static void main(String[] args){\n    ThreadedRSAKey key = new ThreadedRSAKey();\n    for (int i=0; i &quot; + n);\n    System.out.println(&quot;public  =&gt; &quot; + encrypt);\n    System.out.println(&quot;private =&gt; &quot; + decrypt);\n  }\n}\n\n\n * Primes.java Generates large prime numbers.\n***\n\nimport java.math.BigInteger;\n\n\/** A few utilities to generate a large random BigInteger,\n *  and find the next prime number above a given BigInteger.\n \npublic class Primes {\n  \/\/ Note that BigInteger.ZERO was new in JDK 1.2, and 1.1\n  \/\/ code is being used to support the most servlet engines.\n  private static final BigInteger ZERO = new BigInteger(&quot;0&quot;);\n  private static final BigInteger ONE = new BigInteger(&quot;1&quot;);\n  private static final BigInteger TWO = new BigInteger(&quot;2&quot;);\n  \n  \/\/ Likelihood of false prime is less than 1\/2^ERR_VAL\n  \/\/ Assumedly BigInteger uses the Miller-Rabin test or\n  \/\/ equivalent, and thus is NOT fooled by Carmichael numbers.\n  \/\/ See section 33.8 of Cormen et al. Introduction to\n  \/\/ Algorithms for details.\n  private static final int ERR_VAL = 100;\n  \n  public static BigInteger nextPrime(BigInteger start) {\n    if (isEven(start))\n      start = start.add(ONE);\n    else\n      start = start.add(TWO);\n    if (start.isProbablePrime(ERR_VAL))\n      return(start);\n    else\n      return(nextPrime(start));\n  }\n\n  private static boolean isEven(BigInteger n) {\n    return(n.mod(TWO).equals(ZERO));\n  }\n\n  private static StringBuffer[] digits =\n    { new StringBuffer(&quot;0&quot;), new StringBuffer(&quot;1&quot;),\n      new StringBuffer(&quot;2&quot;), new StringBuffer(&quot;3&quot;),\n      new StringBuffer(&quot;4&quot;), new StringBuffer(&quot;5&quot;),\n      new StringBuffer(&quot;6&quot;), new StringBuffer(&quot;7&quot;),\n      new StringBuffer(&quot;8&quot;), new StringBuffer(&quot;9&quot;) };\n\n  private static StringBuffer randomDigit() {\n    int index = (int)Math.floor(Math.random() * 10);\n    return(digits[index]);\n  }\n  \n  public static BigInteger random(int numDigits) {\n    StringBuffer s = new StringBuffer(&quot;&quot;);\n    for(int i=0; i 0)\n      numDigits = Integer.parseInt(args[0]);\n    else\n      numDigits = 150;\n    BigInteger start = random(numDigits);\n    for(int i=0; i&lt;50; i++) {\n      start = nextPrime(start);\n      System.out.println(&quot;Prime &quot; + i + &quot; = &quot; + start);\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=10296<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>ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method. In this example, RSAKey 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 method in RSAKey to a &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=27186\">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-27186","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":10262,"url":"http:\/\/bangla.sitestree.com\/?p=10262","url_meta":{"origin":27186,"position":0},"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":10250,"url":"http:\/\/bangla.sitestree.com\/?p=10250","url_meta":{"origin":27186,"position":1},"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":27176,"url":"http:\/\/bangla.sitestree.com\/?p=27176","url_meta":{"origin":27186,"position":2},"title":"Creates and starts three threaded objects #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","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;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":27186,"position":3},"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":10256,"url":"http:\/\/bangla.sitestree.com\/?p=10256","url_meta":{"origin":27186,"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":27180,"url":"http:\/\/bangla.sitestree.com\/?p=27180","url_meta":{"origin":27186,"position":5},"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":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27186","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=27186"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27186\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27186"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}