{"id":578,"date":"2023-10-02T13:45:21","date_gmt":"2023-10-02T13:45:21","guid":{"rendered":"http:\/\/emacslisp.com\/?p=578"},"modified":"2023-10-02T13:46:37","modified_gmt":"2023-10-02T13:46:37","slug":"java-thread-calculate-result-base1-power1-base2-power2%ef%bf%bc","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=578","title":{"rendered":"Java Thread &#8211; Calculate result = ( base1 ^ power1 ) + (base2 ^ power2)"},"content":{"rendered":"\n<p>Idea is to use two java threads, one thread calculates base1 ^ power1 using BigInteger and another thread calculates base2 ^ power2. Use thread.join() to wait for both thread to complete and sum result from both thread.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.dw.thread;\n\nimport java.math.BigInteger;\n\npublic class ComplexCalculation {\n\n    public BigInteger calculateResult(BigInteger base1, BigInteger power1, BigInteger base2, BigInteger power2) throws InterruptedException {\n        BigInteger result;\n        \/*\n            Calculate result = ( base1 ^ power1 ) + (base2 ^ power2).\n            Where each calculation in (..) is calculated on a different thread\n        *\/\n\n        PowerCalculatingThread thread1 = new PowerCalculatingThread(base1, power1);\n        PowerCalculatingThread thread2 = new PowerCalculatingThread(base2, power2);\n\n        thread1.start();\n        thread2.start();\n\n        thread1.join();   \/\/ important: wait thread to complete\n        thread2.join();  \n        result = thread1.getResult().add(thread2.getResult()); \/\/ add two result\n\n        return result;\n    }\n\n    private static class PowerCalculatingThread extends Thread {\n        private BigInteger result = BigInteger.ONE;\n        private BigInteger base;\n        private BigInteger power;\n\n        public PowerCalculatingThread(BigInteger base, BigInteger power) {\n            this.base = base;\n            this.power = power;\n        }\n\n        @Override\n        public void run() {\n           \/*\n           Implement the calculation of result = base ^ power\n           *\/\n            for (BigInteger i = BigInteger.ZERO; i.compareTo(power) != 0; i = i.add(BigInteger.ONE)) {\n                result = result.multiply(base);\n            }\n        }\n\n        public BigInteger getResult() { return result; }\n    }\n    public static void main(String&#91;] args) throws InterruptedException {\n        ComplexCalculation c = new ComplexCalculation();\n        BigInteger result = c.calculateResult(new BigInteger(\"123\"), new BigInteger(\"10\"), new BigInteger(\"456\"), new BigInteger(\"20\"));\n\n        System.out.println(result);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Idea is to use two java threads, one thread calculates base1 ^ power1 using BigInteger and another thread calculates base2 ^ power2. Use thread.join() to wait for both thread to complete and sum result from both thread.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-578","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=578"}],"version-history":[{"count":3,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions"}],"predecessor-version":[{"id":581,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions\/581"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}