import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.*;
public class App{
public final static BlockingQueue<String> queue = new ArrayBlockingQueue<String>(20);
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(6); // +1 thread for producer
for(int i=0; i<20; ++i){queue.put("Hello");}
service.submit(new Producer(service, queue)).get(); // Wait until producer exits
} finally {
if (null != service) {
service.shutdown();
System.
out.
printf("[%s] Awaiting termination...%n", threadName
); try {
service.awaitTermination(1, TimeUnit.HOURS);
System.
err.
println("Failed to terminate worker"); }
}
}
System.
out.
printf("[%s] Done%n", threadName
); }
}
public Worker
(String message
) { this.message = message;
}
@Override
public void run() {
ThreadLocalRandom random = ThreadLocalRandom.current();
try {
System.
out.
printf("[%s] Sending message '%s'...%n", name, message
); Thread.
sleep(random.
nextInt(50,
300)); System.
out.
printf("[%s] Message '%s' successfully sent!%n", name, message
); System.
err.
printf("[%s] Received interrupt signal, exiting...%n", name
); }
}
}
private final BlockingQueue<String> queue;
private ExecutorService service;
Producer(ExecutorService service, BlockingQueue<String> queue) {
this.service = service;
this.queue = queue;
}
@Override
public void run() {
System.
out.
printf("[%s] Producer started. Enter \"exit\" to stop, or another string to " + "send it over message queue%n", threadName);
while(!(Thread.
currentThread().
isInterrupted())){ try{
String message
= queue.
take(); service.submit(new Worker(message));
System.
out.
printf("[%s] IOException in producer, exiting...", threadName
); }
}
System.
out.
printf("[%s] Producer shutdown", threadName
); }
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklPRXhjZXB0aW9uOwppbXBvcnQgamF2YS5pby5JbnB1dFN0cmVhbVJlYWRlcjsKaW1wb3J0IGphdmEudXRpbC5jb25jdXJyZW50Lio7CgpwdWJsaWMgY2xhc3MgQXBwewogICAgcHVibGljIGZpbmFsIHN0YXRpYyBCbG9ja2luZ1F1ZXVlPFN0cmluZz4gcXVldWUgPSBuZXcgQXJyYXlCbG9ja2luZ1F1ZXVlPFN0cmluZz4oMjApOwogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIEV4ZWN1dGlvbkV4Y2VwdGlvbiwgSW50ZXJydXB0ZWRFeGNlcHRpb24gewogICAgICAgIEV4ZWN1dG9yU2VydmljZSBzZXJ2aWNlID0gbnVsbDsKICAgICAgICBTdHJpbmcgdGhyZWFkTmFtZSA9IFRocmVhZC5jdXJyZW50VGhyZWFkKCkuZ2V0TmFtZSgpOwogICAgICAgIHRyeSB7CiAgICAgICAgICAgIHNlcnZpY2UgPSBFeGVjdXRvcnMubmV3Rml4ZWRUaHJlYWRQb29sKDYpOyAvLyArMSB0aHJlYWQgZm9yIHByb2R1Y2VyCiAgICAgICAgICAgIGZvcihpbnQgaT0wOyBpPDIwOyArK2kpe3F1ZXVlLnB1dCgiSGVsbG8iKTt9CiAgICAgICAgICAgIHNlcnZpY2Uuc3VibWl0KG5ldyBQcm9kdWNlcihzZXJ2aWNlLCBxdWV1ZSkpLmdldCgpOyAvLyBXYWl0IHVudGlsIHByb2R1Y2VyIGV4aXRzCiAgICAgICAgfSBmaW5hbGx5IHsKICAgICAgICAgICAgaWYgKG51bGwgIT0gc2VydmljZSkgewogICAgICAgICAgICAgICAgc2VydmljZS5zaHV0ZG93bigpOwogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIlslc10gQXdhaXRpbmcgdGVybWluYXRpb24uLi4lbiIsIHRocmVhZE5hbWUpOwogICAgICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICAgICAgICBzZXJ2aWNlLmF3YWl0VGVybWluYXRpb24oMSwgVGltZVVuaXQuSE9VUlMpOwogICAgICAgICAgICAgICAgfSBjYXRjaCAoSW50ZXJydXB0ZWRFeGNlcHRpb24gZSkgewogICAgICAgICAgICAgICAgICAgIFN5c3RlbS5lcnIucHJpbnRsbigiRmFpbGVkIHRvIHRlcm1pbmF0ZSB3b3JrZXIiKTsKICAgICAgICAgICA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