import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.*;
public class App {
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(6); // +1 thread for producer
service.submit(new Producer(service)).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(5000,
30000)); System.
out.
printf("[%s] Message '%s' successfully sent!%n", name, message
); System.
err.
printf("[%s] Received interrupt signal, exiting...%n", name
); }
}
}
private ExecutorService service;
Producer(ExecutorService service) {
this.service = service;
}
@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);
try {
while (!"exit".equals(input)) {
input = br.readLine();
if (!"exit".equals(input)) {
service.submit(new Worker(input));
}
}
System.
out.
printf("[%s] IOException in producer, exiting...", threadName
); }
System.
out.
printf("[%s] Producer shutdown", threadName
); }
}
CmltcG9ydCBqYXZhLmlvLkJ1ZmZlcmVkUmVhZGVyOwppbXBvcnQgamF2YS5pby5JT0V4Y2VwdGlvbjsKaW1wb3J0IGphdmEuaW8uSW5wdXRTdHJlYW1SZWFkZXI7CmltcG9ydCBqYXZhLnV0aWwuY29uY3VycmVudC4qOwoKcHVibGljIGNsYXNzIEFwcCB7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgRXhlY3V0aW9uRXhjZXB0aW9uLCBJbnRlcnJ1cHRlZEV4Y2VwdGlvbiB7CiAgICAgICAgRXhlY3V0b3JTZXJ2aWNlIHNlcnZpY2UgPSBudWxsOwogICAgICAgIFN0cmluZyB0aHJlYWROYW1lID0gVGhyZWFkLmN1cnJlbnRUaHJlYWQoKS5nZXROYW1lKCk7CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgc2VydmljZSA9IEV4ZWN1dG9ycy5uZXdGaXhlZFRocmVhZFBvb2woNik7IC8vICsxIHRocmVhZCBmb3IgcHJvZHVjZXIKICAgICAgICAgICAgc2VydmljZS5zdWJtaXQobmV3IFByb2R1Y2VyKHNlcnZpY2UpKS5nZXQoKTsgLy8gV2FpdCB1bnRpbCBwcm9kdWNlciBleGl0cwogICAgICAgIH0gZmluYWxseSB7CiAgICAgICAgICAgIGlmIChudWxsICE9IHNlcnZpY2UpIHsKICAgICAgICAgICAgICAgIHNlcnZpY2Uuc2h1dGRvd24oKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRmKCJbJXNdIEF3YWl0aW5nIHRlcm1pbmF0aW9uLi4uJW4iLCB0aHJlYWROYW1lKTsKICAgICAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgICAgICAgc2VydmljZS5hd2FpdFRlcm1pbmF0aW9uKDEsIFRpbWVVbml0LkhPVVJTKTsKICAgICAgICAgICAgICAgIH0gY2F0Y2ggKEludGVycnVwdGVkRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0uZXJyLnByaW50bG4oIkZhaWxlZCB0byB0ZXJtaW5hdGUgd29ya2VyIik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIlslc10gRG9uZSVuIiwgdGhyZWFkTmFtZSk7CiAgICB9Cn0KCmNsYXNzIFdvcmtlciBpbXBsZW1lbnRzIFJ1bm5hYmxlIHsKICAgIHByaXZhdGUgU3RyaW5nIG1lc3NhZ2U7CgogICAgcHVibGljIFdvcmtlcihTdHJpbmcgbWVzc2FnZSkgewogICAgICAgIHRoaXMubWVzc2FnZSA9IG1lc3NhZ2U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m9kdWNlciBzdGFydGVkLiBFbnRlciBcImV4aXRcIiB0byBzdG9wLCBvciBhbm90aGVyIHN0cmluZyB0byAiICsKICAgICAgICAgICAgICAgICJzZW5kIGl0IG92ZXIgbWVzc2FnZSBxdWV1ZSVuIiwgdGhyZWFkTmFtZSk7CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgQnVmZmVyZWRSZWFkZXIgYnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKFN5c3RlbS5pbikpOwogICAgICAgICAgICBTdHJpbmcgaW5wdXQgPSAiIjsKICAgICAgICAgICAgd2hpbGUgKCEiZXhpdCIuZXF1YWxzKGlucHV0KSkgewogICAgICAgICAgICAgICAgaW5wdXQgPSBici5yZWFkTGluZSgpOwogICAgICAgICAgICAgICAgaWYgKCEiZXhpdCIuZXF1YWxzKGlucHV0KSkgewogICAgICAgICAgICAgICAgICAgIHNlcnZpY2Uuc3VibWl0KG5ldyBXb3JrZXIoaW5wdXQpKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0gY2F0Y2ggKElPRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIlslc10gSU9FeGNlcHRpb24gaW4gcHJvZHVjZXIsIGV4aXRpbmcuLi4iLCB0aHJlYWROYW1lKTsKICAgICAgICB9CiAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIlslc10gUHJvZHVjZXIgc2h1dGRvd24iLCB0aHJlYWROYW1lKTsKICAgIH0KfQo=