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..72a0a48
--- /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..668048d
--- /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/Some different algorithms to calculate pi.pdf b/Some different algorithms to calculate pi.pdf
new file mode 100644
index 0000000..17034ce
Binary files /dev/null and b/Some different algorithms to calculate pi.pdf differ
diff --git a/src/main/java/sbu/cs/CalculatePi/PiCalculator.java b/src/main/java/sbu/cs/CalculatePi/PiCalculator.java
index 3ebc56c..e31b32c 100644
--- a/src/main/java/sbu/cs/CalculatePi/PiCalculator.java
+++ b/src/main/java/sbu/cs/CalculatePi/PiCalculator.java
@@ -1,27 +1,84 @@
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 {
+ static class PiCalculatorTask implements Runnable {
+ private int finalI;
+ public static BigDecimal pi;
+ MathContext mc = new MathContext(1000);
+
+ public PiCalculatorTask(int finalI) {
+ //کانستراکتور
+ this.finalI = finalI;
+ }
+ @Override
+ public void run() {
+ //مقدار دهی اولیه
+ BigDecimal Consequent1 = new BigDecimal(8*finalI + 1);
+ BigDecimal Consequent2 = new BigDecimal(8*finalI + 4);
+ BigDecimal Consequent3 = new BigDecimal(8*finalI + 5);
+ BigDecimal Consequent4 = new BigDecimal(8*finalI + 6);
+ BigDecimal Consequent5 = new BigDecimal(16).pow(finalI);
- /**
- * 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.
+ BigDecimal numerator1 = new BigDecimal(-1);
+ BigDecimal numerator2 = new BigDecimal(-1);
+ BigDecimal numerator3 = new BigDecimal(-2);
+ BigDecimal numerator4 = new BigDecimal(4);
- * 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.
+ numerator1 = numerator1.divide(Consequent3 , mc);
+ numerator2 = numerator2.divide(Consequent4 , mc);
+ numerator3 = numerator3.divide(Consequent2 , mc);
+ numerator4 = numerator4.divide(Consequent1 , mc);
- * @param floatingPoint the exact number of digits after the floating point
- * @return pi in string format (the string representation of the BigDecimal object)
- */
+ BigDecimal term = ((numerator4.add(numerator3)).add(numerator1)).add(numerator2);
+ term = term.divide(Consequent5);
+ addResult(term);
+
+// synchronized (pi){
+// pi = pi.add(term);
+// }
+ }
+ }
public String calculate(int floatingPoint)
{
- // TODO
- return null;
+ //ایجاد تردپول
+ ExecutorService executor = Executors.newFixedThreadPool(10);
+
+ for (int i = 0; i < 1000; i++) {
+ PiCalculatorTask task = new PiCalculatorTask(i);
+ executor.execute(task);
+ }
+
+ executor.shutdown();
+ try{
+ executor.awaitTermination(Long.MAX_VALUE , TimeUnit.MILLISECONDS);
+
+ }
+ catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ pi = pi.setScale(floatingPoint , RoundingMode.DOWN);
+ return pi.toString();
+ }
+ public static BigDecimal pi = new BigDecimal(0);
+ public static synchronized void addResult(BigDecimal term){
+ //محاسبه نهایی عدد پی
+ pi = pi.add(term);
}
public static void main(String[] args) {
- // Use the main function to test the code yourself
+ //امتحان کردن کد
+ PiCalculator piCalculator = new PiCalculator();
+ String s = piCalculator.calculate(2);
+ System.out.println(s);
}
-}
+
+
+ }
\ No newline at end of file
diff --git a/src/main/java/sbu/cs/Semaphore/Controller.java b/src/main/java/sbu/cs/Semaphore/Controller.java
index b29365e..9a9096f 100644
--- a/src/main/java/sbu/cs/Semaphore/Controller.java
+++ b/src/main/java/sbu/cs/Semaphore/Controller.java
@@ -1,5 +1,8 @@
+
package sbu.cs.Semaphore;
+import java.util.concurrent.Semaphore;
+
public class Controller {
/**
@@ -19,11 +22,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");
+ final Semaphore semaphore = new Semaphore(2);
+ Operator operator1 = new Operator("operator1" , semaphore);
+ Operator operator2 = new Operator("operator2" , semaphore);
+ Operator operator3 = new Operator("operator3" , semaphore);
+ Operator operator4 = new Operator("operator4" , semaphore);
+ Operator operator5 = new Operator("operator5" , semaphore);
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..5b719de 100644
--- a/src/main/java/sbu/cs/Semaphore/Operator.java
+++ b/src/main/java/sbu/cs/Semaphore/Operator.java
@@ -1,21 +1,29 @@
package sbu.cs.Semaphore;
-public class Operator extends Thread {
+import java.util.concurrent.Semaphore;
- public Operator(String name) {
- super(name);
+public class Operator extends Thread {
+ private String name;
+ private Semaphore semaphore;
+ public Operator(String name, Semaphore semaphore) {
+ this.name = name;
+ this.semaphore = semaphore;
}
-
@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 {
+ semaphore.acquire();
+ System.out.println("Operator [" + name + "] accessed.");
+ for (int i = 0; i < 10; i++)
+ {
+ Resource.accessResource();
sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
}
+ System.out.println("Operator [" + name + "] exited .");
+ semaphore.release();
+ }
+ catch (InterruptedException e) {
+ e.printStackTrace();
}
}
-}
+}
\ No newline at end of file