-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample.java
More file actions
114 lines (80 loc) · 2.98 KB
/
Example.java
File metadata and controls
114 lines (80 loc) · 2.98 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* 5.
java.lang.InterruptedException :
Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity.
Join() :
join(0) or join() ==> waiting state
join(ms) ==> time waiting state
Interrupt:
soft-interrupt i.e. different from hardware interrupt
*/
import java.util.*;
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Thread> threads = new ArrayList<>();
Thread stats = new Thread(() -> threadStatus(threads));
stats.setDaemon(true);
stats.start();
while (true) {
System.out.print("Enter n & get nth prime number : ");
int num = sc.nextInt();
if (num == 0) {
System.out.println("Waiting for all threads to finish...");
waitForThreads(threads);
System.out.println("Done! "+threads.size()+" primes calculated");
stats.interrupt();
if(stats.isInterrupted()){
// To clean the resources
}
break;
}
Thread t = new Thread(() -> calculatePrime(num));
threads.add(t);
t.start();
try {
t.join(100); // waits at most 100 ms for thread to die else it will continue
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void waitForThreads(List<Thread> threads) {
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*
Barrier Synchronization :
In parallel computing, a barrier is a type of synchronization method where it enables multiple threads to wait until all threads have reached a particular point of execution(barrier) before any thread(main-thread) continues.
*/
}
private static void calculatePrime(int num) {
int numOfPrimeFound = 1;
int number = 2;
int i;
while (numOfPrimeFound < num) {
number++;
for (i = 2; i * i <= number && number % i != 0; i++)
;
if (i * i > number)
numOfPrimeFound++;
}
System.out.println(num + "th prime : " + number);
}
public static void threadStatus(List<Thread> threads) {
while (true) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("No more statistics...");
break;
}
System.out.print("\nThread Status : ");
threads.forEach(t -> System.out.print(t.getState() + " "));
System.out.println();
}
}
}