Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Some different algorithms to calculate pi.pdf
Binary file not shown.
85 changes: 71 additions & 14 deletions src/main/java/sbu/cs/CalculatePi/PiCalculator.java
Original file line number Diff line number Diff line change
@@ -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);
}
}


}
14 changes: 9 additions & 5 deletions src/main/java/sbu/cs/Semaphore/Controller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

package sbu.cs.Semaphore;

import java.util.concurrent.Semaphore;

public class Controller {

/**
Expand All @@ -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();
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/sbu/cs/Semaphore/Operator.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}