forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyLine.java
More file actions
92 lines (69 loc) · 3 KB
/
Copy pathAssemblyLine.java
File metadata and controls
92 lines (69 loc) · 3 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
package modern.challenge;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class AssemblyLine {
private AssemblyLine() {
throw new AssertionError("There is a single assembly line!");
}
private static final Logger logger = Logger.getLogger(AssemblyLine.class.getName());
private static final int PROCESSORS = Runtime.getRuntime().availableProcessors();
private static final int MAX_PROD_BULBS = 15_000_000;
private static final Random rnd = new Random();
private static final BlockingQueue<String> queue = new LinkedBlockingQueue<>();
private static final Consumer consumer = new Consumer();
private static long startTime;
private static ExecutorService consumerService;
private static class Consumer implements Runnable {
@Override
public void run() {
String bulb = queue.poll();
if (bulb != null) {
// process the bulb here
// logger.info(() -> "Packed: " + bulb + " by consumer: "
// + Thread.currentThread().getName());
}
if (queue.isEmpty()) {
logger.info(() -> "The consumers team packed all bulbs in "
+ (System.currentTimeMillis() - startTime) + " ms");
logger.info("Note: It is possible to see the above message multiple times...");
System.exit(0);
}
}
}
public static void startAssemblyLine() {
simulatingProducers();
startConsumers();
}
private static void startConsumers() {
logger.info(() -> "We have a consumers team of "
+ PROCESSORS + " members ...");
consumerService = Executors.newWorkStealingPool();
// consumerService = Executors.newCachedThreadPool();
// consumerService = Executors.newWorkStealingPool(PROCESSORS);
// consumerService = Executors.newFixedThreadPool(PROCESSORS);
int queueSize = queue.size();
startTime = System.currentTimeMillis();
for (int i = 0; i < queueSize; i++) {
consumerService.execute(consumer);
}
consumerService.shutdown();
try {
consumerService.awaitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Logger.getLogger(AssemblyLine.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void simulatingProducers() {
logger.info("Simulating the job of the producers overnight ...");
logger.info(() -> "The producers checked " + MAX_PROD_BULBS + " bulbs ...");
for (int i = 0; i < MAX_PROD_BULBS; i++) {
queue.offer("bulb-" + rnd.nextInt(1000));
}
}
}