-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcPI2Threads.java
More file actions
36 lines (36 loc) · 947 Bytes
/
CalcPI2Threads.java
File metadata and controls
36 lines (36 loc) · 947 Bytes
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
33
34
35
36
// CalcPI2.java
class CalcPI2Threads
{
public static void main (String [] args)
{
MyThread mt = new MyThread ();
mt.start ();
// try
// {
// mt.join();//waits for mt to terminate before the next line of code is exechuted.
// }
// catch (InterruptedException e)
// {
// }
System.out.println ("pi = " + mt.pi);
}
}
class MyThread extends Thread
{
boolean negative = true;
double pi; // Initializes to 0.0, by default
public void run ()
{
for (int i = 3; i < 100000; i += 2)
{
if (negative)
pi -= (1.0 / i);
else
pi += (1.0 / i);
negative = !negative;
}
pi += 1.0;
pi *= 4.0;
System.out.println ("Finished calculating PI");
}
}