Online Java Compiler By
JavaTpoint.com
import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ExecutorCallableExample2 implements Callable
{ @Override public String call() throws Exception { Thread.sleep(500); return Thread.currentThread().getName(); } public static void main(String args[]){ ExecutorService executor = Executors.newFixedThreadPool(20); List
> lst = new ArrayList
>(); Callable
clbl = new ExecutorCallableExample1(); int i =0; while( i < 5){ Future
ftr = executor.submit(clbl); lst.add(ftr); i++; } for(Future
fut : lst){ try { System.out.println( "::"+fut.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } executor.shutdown(); } }
Output