package com.example.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		// FutureTask is a concrete class that
		// implements both Runnable and Callable
		// create 5 future task -> get 5 random numbers
		FutureTask<Integer>[] randomNumberTasks = new FutureTask[5];

		// create tasks, assign callable and start it
		for (int i = 0; i < 5; i++) {
			Callable<Integer> callable = new MyCallable();

			// create the future task and assign a callable
			randomNumberTasks[i] = new FutureTask<Integer>(callable);

			// as it implements Runnable, run the task
//			randomNumberTasks[i].run(); // start the thread
			
			// alternatively - create thread and run the job
			Thread t = new Thread(randomNumberTasks[i]);
			t.start();
			
		}

		// loop to retrieve the result
		for (int i = 0; i < 5; i++) {
			// as it implements Future, we can call get() to obtain the result
			System.out.println(randomNumberTasks[i].get());
			
			/*
			 * This methods blocks until the result is obtained.
			 * The get method can throw checked exception, 
			 * like when it is interrupted.
			 * This is the reason for adding the throws clause.
			 */
		}
	}

}
