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
104 lines (76 loc) · 3.18 KB
/
Copy pathAssemblyLine.java
File metadata and controls
104 lines (76 loc) · 3.18 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
package modern.challenge;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
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 = 100;
private static final Random rnd = new Random();
private static final BlockingQueue<String> queue = new LinkedBlockingQueue<>();
private static final Consumer consumer = new Consumer();
private static ExecutorService consumerService;
private static class Consumer implements Callable {
@Override
public String call() throws InterruptedException {
String bulb = queue.poll();
Thread.sleep(100);
if (bulb != null) {
logger.info(() -> "Packed: " + bulb + " by consumer: "
+ Thread.currentThread().getName());
return bulb;
}
return "";
}
}
public static void startAssemblyLine() {
simulatingProducers();
startConsumers();
}
@SuppressWarnings("unchecked")
private static void startConsumers() {
logger.info(() -> "We have a consumers team of "
+ PROCESSORS + " members ...");
consumerService = Executors.newWorkStealingPool();
// consumerService = Executors.newSingleThreadExecutor();
// consumerService = Executors.newWorkStealingPool(PROCESSORS);
// consumerService = Executors.newFixedThreadPool(PROCESSORS);
int queueSize = queue.size();
List<Future<String>> futures = new ArrayList<>();
for (int i = 0; i < queueSize; i++) {
futures.add(consumerService.submit(consumer));
}
try {
for (Future<String> future : futures) {
String bulb = future.get();
logger.info(() -> "Future done: " + bulb);
}
consumerService.shutdown();
consumerService.awaitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
logger.severe(() -> "Exception: " + ex);
} catch (ExecutionException ex) {
logger.severe(() -> "Exception: " + ex.getCause());
}
}
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));
}
}
}