I'm new to learning Java multithreading. I started with this simple code but it seems that
submitResults.isDone() never returns true.
I'm expecting that "Task Executed" should be printed as a result but I get an empty console.
Here is my simple class:
public class FuturesExample {
public static void main(String[] args) {
FuturesExample.executeThreadScheduler();
}
static void executeThreadScheduler()
{
ExecutorService service = Executors.newFixedThreadPool(10);
Callable<String> callableTask = ()->{
TimeUnit.MILLISECONDS.sleep(300);
return "Task Executed" ;
};
Future<String> submitResults = service.submit(callableTask);
try {
if(submitResults.isDone())
{
System.out.println(submitResults.get());
}
service.shutdown();
if(!service.awaitTermination(800,TimeUnit.MILLISECONDS))
{
service.shutdownNow();
}
} catch (InterruptedException e) {
service.shutdownNow();
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}