-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPi.java
32 lines (21 loc) · 861 Bytes
/
Pi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.concurrent.Semaphore;
public class Pi {
public static void main(String[] args) throws InterruptedException {
double pi;
double[] total_pi = new double[1];
int n_threads = Runtime.getRuntime().availableProcessors();
long iteracoes = 5_000_000_000L;
Semaphore mutex = new Semaphore(1);
Semaphore lock = new Semaphore(-(n_threads-1));
long t0 = System.nanoTime();
for (int t = 0; t < n_threads; ++t) {
Calc c = new Calc(mutex, lock, total_pi, t * iteracoes / n_threads, (t + 1) * iteracoes / n_threads);
c.start();
}
lock.acquire();
pi = 4*total_pi[0];
long d = System.nanoTime() - t0;
System.out.println("PI:"+ pi);
System.out.println("Tempo de execução: " + d/1.0E9D+"s");
}
}