ExecutorService with Callable and Future

In last post I talked about ExecutorService basics.

Let us take it forward to understand usage of Future keyword. So far we have seen Executor service usage to start executing a thread in controlled way. What if we need to get a returned object from the thread being executed. Callable interface comes to help. Instead of implmenting Runnable, we can implement Callable to create thread. In this case we will override call() method method instead of run().

From Javadocs: The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

The result of callable class is retrived in Future instance. Instead of executor.execute, we will call submit method which forces ExecutorService to return the Future instance. execute can also be used with Runnable instance, but in that case it will return null on completion as run() method cannot return an object.

Further Reading

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

package com.kamalmeet;

import java.util.concurrent.*;

public class CallableTest {
	int num = 1;

	public static void main(String s[]) {
		ExecutorService executor = Executors.newFixedThreadPool(5);
		for (int i = 0; i < 10; i++) {
			Future<CallableTest> ft1 = executor.submit(new RunTest1());
			try {
				CallableTest ct1 = (CallableTest) ft1.get();
				System.out.println("ct1 val:" + ct1.num);
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}		}
		executor.shutdown();
	}
}

class RunTest1 implements Callable<CallableTest> {
	@Override
	public CallableTest call() throws Exception {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		CallableTest ct = new CallableTest();
		ct.num = 10;
		return ct;
	}
}