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