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
Binary file modified .gradle/7.5.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.5.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.5.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.5.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.5.1/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
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.

Binary file added 7 report.pdf
Binary file not shown.
77 changes: 68 additions & 9 deletions src/main/java/sbu/cs/CalculatePi/PiCalculator.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<floatingPoint*4){
PI runnable = new PI(BigDecimal.valueOf(i),floatingPoint);
threadPool.execute(runnable);
i++;
}
threadPool.shutdown();

try {
threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
answer = answer.setScale(floatingPoint, RoundingMode.DOWN);
return String.valueOf(answer);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
return null;
}

public static void main(String[] args) {
// Use the main function to test the code yourself
System.out.println(calculate(2));
System.out.println(calculate(7));
//System.out.println(calculate(100));
//System.out.println(calculate(1000));
}
}
}
12 changes: 10 additions & 2 deletions src/main/java/sbu/cs/PrioritySimulator/BlackThread.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package sbu.cs.PrioritySimulator;
import java.util.concurrent.CountDownLatch;


public class BlackThread extends ColorThread {

private static final String MESSAGE = "hi blues, hi whites!";
private static final String MESSAGE = "hi finished blacks, hi whites!";

private final CountDownLatch blackLatch;
public BlackThread(CountDownLatch blackLatch) {
this.blackLatch = blackLatch;
}

void printMessage() {
super.printMessage(new Message(this.getClass().getName(), getMessage()));
Expand All @@ -15,6 +22,7 @@ String getMessage() {

@Override
public void run() {
// TODO call printMessage
printMessage();
blackLatch.countDown();
}
}
10 changes: 9 additions & 1 deletion src/main/java/sbu/cs/PrioritySimulator/BlueThread.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package sbu.cs.PrioritySimulator;
import java.util.concurrent.CountDownLatch;


public class BlueThread extends ColorThread {

private static final String MESSAGE = "hi finished blacks, hi whites!";

private final CountDownLatch blueLatch;
public BlueThread(CountDownLatch blueLatch) {
this.blueLatch = blueLatch;
}

void printMessage() {
super.printMessage(new Message(this.getClass().getName(), getMessage()));
}
Expand All @@ -15,6 +22,7 @@ String getMessage() {

@Override
public void run() {
// TODO call printMessage
printMessage();
blueLatch.countDown();
}
}
26 changes: 14 additions & 12 deletions src/main/java/sbu/cs/PrioritySimulator/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class Runner {

Expand Down Expand Up @@ -33,31 +34,32 @@ black threads are not prioritised over each other (can swap their execution orde
public void run(int blackCount, int blueCount, int whiteCount) throws InterruptedException {
List<ColorThread> 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) {
Expand All @@ -69,6 +71,6 @@ public List<Message> getMessages() {
}

public static void main(String[] args) {
// Use the main function to test the code yourself

}
}
11 changes: 10 additions & 1 deletion src/main/java/sbu/cs/PrioritySimulator/WhiteThread.java
Original file line number Diff line number Diff line change
@@ -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()));
}
Expand All @@ -15,6 +23,7 @@ String getMessage() {

@Override
public void run() {
// TODO call printMessage
printMessage();
whiteLatch.countDown();
}
}
13 changes: 8 additions & 5 deletions src/main/java/sbu/cs/Semaphore/Controller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sbu.cs.Semaphore;

import java.util.concurrent.Semaphore;

public class Controller {

/**
Expand All @@ -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();
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/sbu/cs/Semaphore/Operator.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 4 additions & 1 deletion src/main/java/sbu/cs/Semaphore/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down