Expert
This code
SimpleDateFormat format = new SimpleDateFormat("DD");
ExecutorService executor = Executors
.newFixedThreadPool(10);
Set<Future<String>> days = Collections
.newSetFromMap(new ConcurrentHashMap<>());
IntStream.range(1, 32)
.forEach(day -> {
Future<String> result = executor.submit(() -> {
Date date = new GregorianCalendar(2021,
Calendar.JANUARY, day).getTime();
return format.format(date);
});
days.add(result);
});
Set<String> results = days.stream()
.map(return result -> {
try {
return result.get();
} catch (InterruptedException | ExecutionException e) {
throw new AssertionError(e);
}
})
.collect(Collectors.toSet());
Author: Clément DevosStatus: PublishedQuestion passed 310 times
Edit
3
Community EvaluationsNo one has reviewed this question yet, be the first!
Similar QuestionsMore questions about Java
1
Write a function that returns the first character of a string in Java1
Java code that replaces keys in a template with their values.1
A Java class that converts Arabic numbers to Roman numerals.1
A code kata is a small exercice thought to train a certain development competence0
Write a thread safe singleton in Java