Код: Выделить всё
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(1);
Callable callable1 = () -> {
Thread.sleep(1000);
System.out.println("task 1");
return 1;
};
Callable callable2 = () -> {
Thread.sleep(1000);
System.out.println("task 2");
return 2;
};
List list = Arrays.asList(callable1 , callable2);
List futures = executor.invokeAll(list , 1, TimeUnit.SECONDS) ;
for(Future f : futures){
try {
System.out.println(f.get());
} catch (Exception e) {
}
}
executor.shutdown();
System.out.println("hello world");
}
Я попытался бросить Interruptexception, CallableException, executionExcect>
Подробнее здесь: https://stackoverflow.com/questions/797 ... ing-method