forked from hansiming/JavaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvenChecker.java
More file actions
55 lines (40 loc) · 1.05 KB
/
EvenChecker.java
File metadata and controls
55 lines (40 loc) · 1.05 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
package com.csdhsm.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Title: EvenChecker.java
* @Package: com.csdhsm.concurrent
* @Description
* @author Han
* @date 2016-4-9 下午8:24:57
* @version V1.0
*/
public class EvenChecker implements Runnable{
private IntGenerator generator;
@SuppressWarnings("unused")
private final int id;
public EvenChecker(IntGenerator generator, int id) {
this.generator = generator;
this.id = id;
}
@Override
public void run() {
while(!generator.isCanceled()){
int val = generator.next();
if(val % 2 != 0){
System.out.println(val + " not even!");
generator.setCanceled(true);
}
}
}
public static void test(IntGenerator generator, int count){
ExecutorService service = Executors.newFixedThreadPool(count);
for(int i = 0 ; i < count ; i++){
service.execute(new EvenChecker(generator, i));
}
service.shutdown();
}
public static void test(IntGenerator generator){
test(generator, 10);
}
}