-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSerialNumberChecker.java
More file actions
31 lines (30 loc) · 998 Bytes
/
SerialNumberChecker.java
File metadata and controls
31 lines (30 loc) · 998 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
// lowlevel/SerialNumberChecker.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Test SerialNumbers implementations for thread-safety
import java.util.concurrent.*;
import onjava.Nap;
public class SerialNumberChecker implements Runnable {
private CircularSet serials = new CircularSet(1000);
private SerialNumbers producer;
public SerialNumberChecker(SerialNumbers producer) {
this.producer = producer;
}
@Override public void run() {
while(true) {
int serial = producer.nextSerialNumber();
if(serials.contains(serial)) {
System.out.println("Duplicate: " + serial);
System.exit(0);
}
serials.add(serial);
}
}
static void test(SerialNumbers producer) {
for(int i = 0; i < 10; i++)
CompletableFuture.runAsync(
new SerialNumberChecker(producer));
new Nap(4, "No duplicates detected");
}
}