diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..58144a2 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..172986e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Sixth-Assignment-Advanced-Multithreading - Shortcut.lnk b/Sixth-Assignment-Advanced-Multithreading - Shortcut.lnk new file mode 100644 index 0000000..683ad24 Binary files /dev/null and b/Sixth-Assignment-Advanced-Multithreading - Shortcut.lnk differ diff --git a/src/main/java/sbu/cs/CalculatePi/PiCalculator.java b/src/main/java/sbu/cs/CalculatePi/PiCalculator.java index 3ebc56c..f9c5abe 100644 --- a/src/main/java/sbu/cs/CalculatePi/PiCalculator.java +++ b/src/main/java/sbu/cs/CalculatePi/PiCalculator.java @@ -1,5 +1,12 @@ package sbu.cs.CalculatePi; +import java.math.BigDecimal; +import java.math.MathContext; +import java.math.RoundingMode; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + public class PiCalculator { /** @@ -11,17 +18,113 @@ public class PiCalculator { * Create as many classes and threads as you need. * Your code must pass all of the test cases provided in the test folder. - * @param floatingPoint the exact number of digits after the floating point + * @param "floatingPoint" the exact number of digits after the floating point * @return pi in string format (the string representation of the BigDecimal object) */ - public String calculate(int floatingPoint) + public static class Calculatepi implements Runnable { + int n; + MathContext mc = new MathContext(10000); + public Calculatepi (int n) { + this.n = n; + } + @Override + public void run() { + BigDecimal sign = new BigDecimal(1); + if (n%2 == 1) { + sign = new BigDecimal(-1); + } + //BigDecimal n = new BigDecimal(m); + BigDecimal numerator = new BigDecimal(1); + BigDecimal denominator = new BigDecimal(1); + BigDecimal a = factorial(6*n); + BigDecimal b = new BigDecimal((545140134*n)+13591409); + numerator = numerator.multiply(sign,mc); + numerator = numerator.multiply(a,mc); + numerator = numerator.multiply(b,mc); + BigDecimal c = factorial(3*n); + BigDecimal d =factorial(n); + d = d.pow(3); + BigDecimal e = new BigDecimal(640320); + e = e.pow(3*n); + BigDecimal f = new BigDecimal(640320); + f = f.pow(3); + MathContext g = new MathContext(10000); + f = f.sqrt(g); + denominator = denominator.multiply(c,mc); + denominator = denominator.multiply(d,mc); + denominator = denominator.multiply(e,mc); + denominator = denominator.multiply(f,mc); + + MathContext mc = new MathContext(10000); + BigDecimal result = numerator.divide(denominator, mc); + addTosum(result); + } + public BigDecimal factorial(int k) { + BigDecimal temp = new BigDecimal(1); + for (int i = 1; i <= k; i++) { + temp = temp.multiply(new BigDecimal(i)); + } + return temp; + } + } + public static BigDecimal sum; + + public static synchronized void addTosum(BigDecimal value) { + sum = sum.add(value); + } + public static String calculate(int floatingPoint) { // TODO - return null; + //MathContext mc = new MathContext(floatingPoint); + ExecutorService threadpool = Executors.newFixedThreadPool(10); + sum = new BigDecimal(0); + + for (int i = 0; i < 1000; i++) { + Calculatepi task = new Calculatepi(i); + threadpool.execute(task); + } + threadpool.shutdown(); + + try { + threadpool.awaitTermination(1000000, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + BigDecimal factor = new BigDecimal(12); + MathContext mc = new MathContext(10000); + sum = sum.multiply(factor,mc); + sum = sum.pow(-1, mc); + sum = sum.setScale(floatingPoint, RoundingMode.DOWN); + String pi = String.valueOf(sum); + pi = pi.substring(0,floatingPoint+2); + return pi; } public static void main(String[] args) { // Use the main function to test the code yourself + /*ExecutorService threadpool = Executors.newFixedThreadPool(6); + sum = new BigDecimal(0); + + for (int i = 0; i < 1000; i++) { + Calculatepi task = new Calculatepi(i); + threadpool.execute(task); + } + threadpool.shutdown(); + + try { + threadpool.awaitTermination(100000, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + BigDecimal factor = new BigDecimal(12); + MathContext mc = new MathContext(10000); + sum = sum.multiply(factor); + sum = sum.pow(-1, mc); + sum = sum.setScale(2, RoundingMode.DOWN); + String pi = String.valueOf(sum); + System.out.println(pi); +*/ + } } diff --git a/src/main/java/sbu/cs/CalculatePi/Report.md b/src/main/java/sbu/cs/CalculatePi/Report.md new file mode 100644 index 0000000..929af25 --- /dev/null +++ b/src/main/java/sbu/cs/CalculatePi/Report.md @@ -0,0 +1,37 @@ +## PiCalculator + +Among the sites, 7learn.com site explained 4 algorithms well + +* Monte-Carlo method to calculate pi + +* A calculation method for the Pi number + +* The infinite sum method in calculating Pi + +* An improved infinite sum method for calculating Pi + +Among these four algorithms, the last algorithm was the most optimal and accurate algorithm + +## About this algorithm: +### Chudnovsky algorithm + +Ramanujan wrote two formulas to calculate pi. The Chanowski brothers, +Gregory (1952) and Dioud (1947) improved Ramanujan's early formulas. + +![img.png](img.png) + +The Chudnovsky algorithm is a fast method for calculating the digits of π, based on Ramanujan's π formulae. Published by the Chudnovsky brothers in 1988, it was used to calculate π to a billion decimal places. + +<<<<<<< HEAD +It was used in the world record calculations of 2.7 trillion digits of π in December 2009, 10 trillion digits in October 2011, 22.4 trillion digits in November 2016, 31.4 trillion digits in September 2018–January 2019, 50 trillion digits on January 29, 2020, 62.8 trillion digits on August 14, 2021, 100 trillion digits on March 21, 2022, and 105 trillion digits on March 14, 2024. + +## Semaphoro + +By searching the internet, sites linkedin.com and geeksforgeeks.org provided a good definition of semaphore. + +* linkedin: In Java's concurrency API, a semaphore is another synchronization tool that simultaneously controls the number of threads accessing a particular resource or section of code. It manages a set of permits; threads must acquire a permit before proceeding. If a permit is available, the thread acquires it and continues execution. If not, the thread is blocked until a permit becomes available or interrupted. + +* geeksforgeeks: A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource. Thus, to access the resource, a thread must be granted a permit from the semaphore. +======= +It was used in the world record calculations of 2.7 trillion digits of π in December 2009, 10 trillion digits in October 2011, 22.4 trillion digits in November 2016, 31.4 trillion digits in September 2018–January 2019, 50 trillion digits on January 29, 2020, 62.8 trillion digits on August 14, 2021, 100 trillion digits on March 21, 2022, and 105 trillion digits on March 14, 2024. +>>>>>>> origin/develop diff --git a/src/main/java/sbu/cs/CalculatePi/img.png b/src/main/java/sbu/cs/CalculatePi/img.png new file mode 100644 index 0000000..dcd74cf Binary files /dev/null and b/src/main/java/sbu/cs/CalculatePi/img.png differ diff --git a/src/main/java/sbu/cs/Semaphore/Controller.java b/src/main/java/sbu/cs/Semaphore/Controller.java index ff27df4..667531c 100644 --- a/src/main/java/sbu/cs/Semaphore/Controller.java +++ b/src/main/java/sbu/cs/Semaphore/Controller.java @@ -1,7 +1,11 @@ package sbu.cs.Semaphore; +import java.util.concurrent.Semaphore; + public class Controller { + public static final Semaphore sem = new Semaphore(2); + /** * A number of operators are trying to access a resource in parallel to each other. * A maximum of 2 operators should access the resource concurrently. Once 2 operators have entered @@ -18,7 +22,7 @@ public class Controller { * Note that every time a thread accesses the resource, you must print its Name and the System Time. */ - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { Operator operator1 = new Operator("operator1"); Operator operator2 = new Operator("operator2"); Operator operator3 = new Operator("operator3"); @@ -35,8 +39,13 @@ public static void main(String[] args) { TODO Use a Semaphore to solve the synchronization problem. Every time a thread accesses the resource, print its Name and the current System Time. - Combine the 4 resulting blocks to create the final matrix product and return it. */ + operator1.join(); + operator2.join(); + operator3.join(); + operator4.join(); + operator5.join(); + } } diff --git a/src/main/java/sbu/cs/Semaphore/Operator.java b/src/main/java/sbu/cs/Semaphore/Operator.java index 33613e6..3baf2ae 100644 --- a/src/main/java/sbu/cs/Semaphore/Operator.java +++ b/src/main/java/sbu/cs/Semaphore/Operator.java @@ -1,5 +1,8 @@ package sbu.cs.Semaphore; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + public class Operator extends Thread { public Operator(String name) { @@ -8,14 +11,27 @@ public Operator(String name) { @Override public void run() { - for (int i = 0; i < 10; i++) - { - Resource.accessResource(); // critical section - a Maximum of 2 operators can access the resource concurrently - try { - sleep(500); - } catch (InterruptedException e) { - e.printStackTrace(); + try { + Controller.sem.acquire(); + for (int i = 0; i < 10; i++) { + Resource.accessResource(); // critical section - a Maximum of 2 operators can access the resource concurrently + try { + sleep(500); + } + catch (InterruptedException e) { + e.printStackTrace(); + } } + LocalTime currentTime = LocalTime.now (); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("HH:mm:ss"); + System.out.println (Thread.currentThread ().getName () + " accessed the resource at " + currentTime.format (formatter)); + } + catch (InterruptedException e) { + System.out.println(e.getMessage()); + } + finally { + Controller.sem.release(); } } }