/* package whatever; // don't place package name! */
import java.time.DayOfWeek;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main
( String[] args
) {
Ideone app = new Ideone();
app.demo();
}
private void demo ( )
{
ExecutorService executorService = Executors.newCachedThreadPool();
"Alice" , DayOfWeek.MONDAY ,
"Bob" , DayOfWeek.TUESDAY
);
ConcurrentMap
< String, DayOfWeek
> map
= new ConcurrentHashMap
<>( inputs
); System.
out.
println( "INFO - Before: map = " + map
);
Callable
< DayOfWeek
> task
= ( ) -> { return map.
computeIfAbsent( "Carol" ,
( String personNameKey
) -> {return repository.
fetchDayOfWeekForPersonName( personNameKey
); } ); }; List < Callable
< DayOfWeek
> > tasks
= List.
of( task , task , task , task , task
); List < Future
< DayOfWeek
> > futures
= List.
of(); try
{
futures = executorService.invokeAll( tasks );
}
{
e.printStackTrace();
}
executorService.shutdown();
try { executorService.
awaitTermination( 10 , TimeUnit.
SECONDS ); } catch ( InterruptedException e
) { e.
printStackTrace(); }
System.
out.
println( "INFO - After: map = " + map
); futures.stream().forEach( dayOfWeekFuture -> {
try
{
System.
out.
println( dayOfWeekFuture.
get() ); }
{
e.printStackTrace();
}
catch ( ExecutionException e )
{
e.printStackTrace();
}
} );
}
{
public DayOfWeek fetchDayOfWeekForPersonName
( final String personName
) {
return DayOfWeek.THURSDAY;
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudGltZS5EYXlPZldlZWs7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5NYXA7CmltcG9ydCBqYXZhLnV0aWwuY29uY3VycmVudC4qOwoKLyogTmFtZSBvZiB0aGUgY2xhc3MgaGFzIHRvIGJlICJNYWluIiBvbmx5IGlmIHRoZSBjbGFzcyBpcyBwdWJsaWMuICovCmNsYXNzIElkZW9uZQp7Ci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aXN0Lm9mKCB0YXNrICwgdGFzayAsIHRhc2sgLCB0YXNrICwgdGFzayApOwogICAgICAgIExpc3QgPCBGdXR1cmUgPCBEYXlPZldlZWsgPiA+IGZ1dHVyZXMgPSBMaXN0Lm9mKCk7CiAgICAgICAgdHJ5CiAgICAgICAgewogICAgICAgICAgICBmdXR1cmVzID0gZXhlY3V0b3JTZXJ2aWNlLmludm9rZUFsbCggdGFza3MgKTsKICAgICAgICB9CiAgICAgICAgY2F0Y2ggKCBJbnRlcnJ1cHRlZEV4Y2VwdGlvbiBlICkKICAgICAgICB7CiAgICAgICAgICAgIGUucHJpbnRTdGFja1RyYWNlKCk7CiAgICAgICAgfQoKICAgICAgICBleGVjdXRvclNlcnZpY2Uuc2h1dGRvd24oKTsKICAgICAgICB0cnkgeyBleGVjdXRvclNlcnZpY2UuYXdhaXRUZXJtaW5hdGlvbiggMTAgLCBUaW1lVW5pdC5TRUNPTkRTICk7IH0gY2F0Y2ggKCBJbnRlcnJ1cHRlZEV4Y2VwdGlvbiBlICkgeyBlLnByaW50U3RhY2tUcmFjZSgpOyB9CgogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbiggIklORk8gLSBBZnRlcjogbWFwID0gIiArIG1hcCApOwogICAgICAgIGZ1dHVyZXMuc3RyZWFtKCkuZm9yRWFjaCggZGF5T2ZXZWVrRnV0dXJlIC0+IHsKICAgICAgICAgICAgdHJ5CiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbiggZGF5T2ZXZWVrRnV0dXJlLmdldCgpICk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY2F0Y2ggKCBJbnRlcnJ1cHRlZEV4Y2VwdGlvbiBlICkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZS5wcmludFN0YWNrVHJhY2UoKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBjYXRjaCAoIEV4ZWN1dGlvbkV4Y2VwdGlvbiBlICkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZS5wcmludFN0YWNrVHJhY2UoKTsKICAgICAgICAgICAgfQogICAgICAgIH0gKTsKICAgIH0KCiAgICBjbGFzcyBSZXBvc2l0b3J5CiAgICB7CiAgICAgICAgcHVibGljIERheU9mV2VlayBmZXRjaERheU9mV2Vla0ZvclBlcnNvbk5hbWUgKCBmaW5hbCBTdHJpbmcgcGVyc29uTmFtZSApCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gRGF5T2ZXZWVrLlRIVVJTREFZOwogICAgICAgIH0KICAgIH0KfQo=