~/ learn/ java-from-zero/ cards/ Callable, Future & ExecutorService
1 of 4

Type a Callable, submit it, and call get()

Type a Callable, submit it, and call get()

Answer

ExecutorService pool = Executors.newFixedThreadPool(2); Future<String> f = pool.submit(() -> "result"); String value = f.get(); pool.shutdown();

submit() accepts both Runnable and Callable. When the generic is Callable<String>, get() returns String. Always shutdown() the pool when finished.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/bf4a596f-f0da-4a21-bcba-f2e1f61f1721/flashcard utf-8 LF