-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEvenChecker.java
More file actions
49 lines (37 loc) · 1.01 KB
/
EvenChecker.java
File metadata and controls
49 lines (37 loc) · 1.01 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
package concurrency;
import java.util.concurrent.*;
/**
* RUN:
* javac concurrency/EvenChecker.java
*
* OUTPUT:
*
*/
public class EvenChecker implements Runnable {
private IntGenerator generator;
private final int id;
public EvenChecker(IntGenerator g, int ident) {
generator = g;
id = ident;
}
public void run() {
while (! generator.isCanceled()) {
int val = generator.next();
if (val % 2 != 0) {
System.out.println(val + " is not even!");
generator.cancel();
}
}
}
public static void test(IntGenerator gp, int count) {
System.out.println("Please press Ctrl-C for exit...");
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < count; i++) {
exec.execute(new EvenChecker(gp, i));
}
exec.shutdown();
}
public static void test(IntGenerator gp) {
test(gp, 10);
}
}