diff --git a/.gradle/7.5.1/executionHistory/executionHistory.bin b/.gradle/7.5.1/executionHistory/executionHistory.bin index 6c54050..40c99dc 100644 Binary files a/.gradle/7.5.1/executionHistory/executionHistory.bin and b/.gradle/7.5.1/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.5.1/executionHistory/executionHistory.lock b/.gradle/7.5.1/executionHistory/executionHistory.lock index 1cb4723..99beef9 100644 Binary files a/.gradle/7.5.1/executionHistory/executionHistory.lock and b/.gradle/7.5.1/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.5.1/fileHashes/fileHashes.bin b/.gradle/7.5.1/fileHashes/fileHashes.bin index 058c3de..8d30f72 100644 Binary files a/.gradle/7.5.1/fileHashes/fileHashes.bin and b/.gradle/7.5.1/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.5.1/fileHashes/fileHashes.lock b/.gradle/7.5.1/fileHashes/fileHashes.lock index 1cba1b5..5c25d6f 100644 Binary files a/.gradle/7.5.1/fileHashes/fileHashes.lock and b/.gradle/7.5.1/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.5.1/fileHashes/resourceHashesCache.bin b/.gradle/7.5.1/fileHashes/resourceHashesCache.bin index d9cd9ab..ca43815 100644 Binary files a/.gradle/7.5.1/fileHashes/resourceHashesCache.bin and b/.gradle/7.5.1/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 4433d4f..b38d113 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin index da69284..a60bda8 100644 Binary files a/.gradle/buildOutputCleanup/outputFiles.bin and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index 0bebf6a..10da010 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ 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/7 report.pdf b/7 report.pdf new file mode 100644 index 0000000..473a724 Binary files /dev/null and b/7 report.pdf differ diff --git a/src/main/java/sbu/cs/CalculatePi/PiCalculator.java b/src/main/java/sbu/cs/CalculatePi/PiCalculator.java index 3ebc56c..1ad7817 100644 --- a/src/main/java/sbu/cs/CalculatePi/PiCalculator.java +++ b/src/main/java/sbu/cs/CalculatePi/PiCalculator.java @@ -1,27 +1,86 @@ package sbu.cs.CalculatePi; -public class PiCalculator { + /** * Calculate pi and represent it as a BigDecimal object with the given floating point number (digits after . ) * There are several algorithms designed for calculating pi, it's up to you to decide which one to implement. - Experiment with different algorithms to find accurate results. - + * Experiment with different algorithms to find accurate results. + *

* You must design a multithreaded program to calculate pi. Creating a thread pool is recommended. * 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 * @return pi in string format (the string representation of the BigDecimal object) */ - public String calculate(int floatingPoint) - { - // TODO +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.concurrent.*; + +public class PiCalculator { + static BigDecimal answer = BigDecimal.ZERO; + + public static class PI implements Runnable { + BigDecimal d; + int floatingPoint; + + + public PI(BigDecimal d, int floatingPoint ) { + this.d = d; + this.floatingPoint = floatingPoint; + } + + public void run() { + BigDecimal factorialD = factorial(d.intValue()); + factorialD = factorialD.pow(2); + BigDecimal powTerm = BigDecimal.valueOf(2).pow(d.intValue() + 1); + BigDecimal numerator = factorialD.multiply(powTerm); + BigDecimal denominator = factorial((2 * d.intValue()) + 1); + BigDecimal result = numerator.divide(denominator, floatingPoint + 2, RoundingMode.DOWN); + sumInc(result); + } + } + + public static BigDecimal factorial(int n) { + if (n == 0) { + return BigDecimal.valueOf(1); + } else { + return BigDecimal.valueOf(n).multiply(factorial(n - 1)); + } + } + + public static synchronized void sumInc(BigDecimal d) { + answer = answer.add(d); + } + + public static String calculate(int floatingPoint) { + answer = BigDecimal.ZERO; + ExecutorService threadPool = Executors.newCachedThreadPool(); + + int i=0; + while (i colorThreads = new ArrayList<>(); - // TODO + CountDownLatch blueLatch = new CountDownLatch(blueCount); + CountDownLatch blackLatch = new CountDownLatch(blackCount); + CountDownLatch whiteLatch = new CountDownLatch(whiteCount); for (int i = 0; i < blackCount; i++) { - BlackThread blackThread = new BlackThread(); + BlackThread blackThread = new BlackThread(blackLatch); colorThreads.add(blackThread); blackThread.start(); } - - // TODO + blackLatch.await(); for (int i = 0; i < blueCount; i++) { - BlueThread blueThread = new BlueThread(); + BlueThread blueThread = new BlueThread(blueLatch); colorThreads.add(blueThread); blueThread.start(); } - - // TODO + blackLatch.await(); for (int i = 0; i < whiteCount; i++) { - WhiteThread whiteThread = new WhiteThread(); - colorThreads.add(whiteThread); - whiteThread.start(); + WhiteThread whitThread = new WhiteThread(whiteLatch); + colorThreads.add(whitThread); + whitThread.start(); } + blackLatch.await(); + - // TODO } synchronized public static void addToList(Message message) { @@ -69,6 +71,6 @@ public List getMessages() { } public static void main(String[] args) { - // Use the main function to test the code yourself + } } diff --git a/src/main/java/sbu/cs/PrioritySimulator/WhiteThread.java b/src/main/java/sbu/cs/PrioritySimulator/WhiteThread.java index 8a8d7f0..9e820d3 100644 --- a/src/main/java/sbu/cs/PrioritySimulator/WhiteThread.java +++ b/src/main/java/sbu/cs/PrioritySimulator/WhiteThread.java @@ -1,9 +1,17 @@ package sbu.cs.PrioritySimulator; +import java.util.concurrent.CountDownLatch; + public class WhiteThread extends ColorThread { private static final String MESSAGE = "hi finished blacks, hi finished blues!"; + private final CountDownLatch whiteLatch; + + public WhiteThread(CountDownLatch whiteLatch) { + this.whiteLatch = whiteLatch; + } + void printMessage() { super.printMessage(new Message(this.getClass().getName(), getMessage())); } @@ -15,6 +23,7 @@ String getMessage() { @Override public void run() { - // TODO call printMessage + printMessage(); + whiteLatch.countDown(); } } diff --git a/src/main/java/sbu/cs/Semaphore/Controller.java b/src/main/java/sbu/cs/Semaphore/Controller.java index 1ca61d4..db578f2 100644 --- a/src/main/java/sbu/cs/Semaphore/Controller.java +++ b/src/main/java/sbu/cs/Semaphore/Controller.java @@ -1,5 +1,7 @@ package sbu.cs.Semaphore; +import java.util.concurrent.Semaphore; + public class Controller { /** @@ -18,11 +20,12 @@ public class Controller { */ public static void main(String[] args) { - Operator operator1 = new Operator("operator1"); - Operator operator2 = new Operator("operator2"); - Operator operator3 = new Operator("operator3"); - Operator operator4 = new Operator("operator4"); - Operator operator5 = new Operator("operator5"); + Semaphore s = new Semaphore(2,true); + Operator operator1 = new Operator(s,"operator1"); + Operator operator2 = new Operator(s,"operator2"); + Operator operator3 = new Operator(s,"operator3"); + Operator operator4 = new Operator(s,"operator4"); + Operator operator5 = new Operator(s,"operator5"); operator1.start(); operator2.start(); diff --git a/src/main/java/sbu/cs/Semaphore/Operator.java b/src/main/java/sbu/cs/Semaphore/Operator.java index 33613e6..8e7176f 100644 --- a/src/main/java/sbu/cs/Semaphore/Operator.java +++ b/src/main/java/sbu/cs/Semaphore/Operator.java @@ -1,21 +1,28 @@ package sbu.cs.Semaphore; +import java.util.concurrent.Semaphore; + public class Operator extends Thread { - public Operator(String name) { + private Semaphore s; + public Operator(Semaphore s ,String name) { + super(name); + this.s=s; } @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 { + try { + s.acquire(); + for (int i = 0; i < 10; i++) { + Resource.accessResource(getName());// critical section - a Maximum of 2 operators can access the resource concurrently sleep(500); - } catch (InterruptedException e) { - e.printStackTrace(); } } + catch (InterruptedException e) { + e.printStackTrace(); + } + s.release(); } } diff --git a/src/main/java/sbu/cs/Semaphore/Resource.java b/src/main/java/sbu/cs/Semaphore/Resource.java index 78a8344..8c9d1ce 100644 --- a/src/main/java/sbu/cs/Semaphore/Resource.java +++ b/src/main/java/sbu/cs/Semaphore/Resource.java @@ -2,9 +2,12 @@ public class Resource { - public static void accessResource() { + public static void accessResource(String s) { + String name = s; try { Thread.sleep(100); + System.out.println(name); + System.out.println(System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); }