package com.example.lambda;

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 ExecutorCallableExample {

	public static void main(String[] args) throws InterruptedException, ExecutionException  {
		ExecutorService executor = Executors.newFixedThreadPool(3);
		
		// define the callable
		Callable<String> task = () -> {
			Thread.sleep(1000);
			String name = Thread.currentThread().getName();
			return "Task executed by thread: " + name;
		};
		
		// submit task to executor and get result
		Future<String> result = executor.submit(task);
		
		System.out.println("Task submitted.");
		// this line below will block until result is ready
		System.out.println("Result: " + result.get()); 
		System.out.println("Task completed.");
		
		executor.shutdown();
	}

}
