Created
November 2, 2021 06:05
-
-
Save h-hub/dcf0686a8fc9fed76e7307575208f583 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CallableExample { | |
public static void main(String[] args) { | |
ExecutorService executor = Executors.newFixedThreadPool(10); | |
List<Future<String>> list = new ArrayList<>(); | |
Callable<String> callable = new HelloCallable(); | |
for(int i=0; i< 100; i++){ | |
Future<String> future = executor.submit(callable); | |
list.add(future); | |
} | |
for(Future<String> fut : list){ | |
try { | |
System.out.println(fut.get()); | |
} catch (InterruptedException | ExecutionException e) { | |
System.out.println("Exception thrown: "+e.getMessage()); | |
} | |
} | |
executor.shutdown(); | |
} | |
} | |
class HelloCallable implements Callable<String> { | |
@Override | |
public String call() throws Exception { | |
LocalDateTime now = LocalDateTime.now(); | |
long nanos = now.toInstant(OffsetDateTime.now().getOffset()) | |
.toEpochMilli(); | |
if(nanos % 2 == 0){ | |
throw new Exception("even time: "+nanos); | |
} | |
return LocalDateTime.now().toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment