Tuesday, September 13, 2011

Java Threads: Future & Callable

package main.java.threads;

import java.util.concurrent.*;
import java.util.List;
import java.util.ArrayList;

public class Future_Callable {

	private static final int nThreads = 3;
	public static ExecutorService e = Executors.newFixedThreadPool(nThreads);

	public static void main(String[] args) throws InterruptedException, ExecutionException {

		List<String> outputs = new ArrayList<String>();

		for (int i = 0; i < 10; i++) {
			MyCallable myCallable = new MyCallable();
			// On the executor you can use the method submit to "submit" (instead of .execute(..)) a Callable and to get a future. 
			// To retrieve the result of the future use the get() 
			Future<String> future = e.submit(myCallable);

		for (String o : outputs) {

// For threads to return a computed result/ per thread- use java.util.concurrent.Callable. 
// Callable uses generic to define the type of object which is returned.
// It implements Callable<T> instead of Runnable in normal thread
// it should provide an implementation of call() function instead of run()
// It should return a value

class MyCallable implements Callable<String> {

	private int num;

	public void setNum(int n) {
		this.num = n;

	public String call() {
		int sum = 0;
		for (int i = 1; i <= num; i++) {
			sum += i;
		return "Sum of " + this.num + " numbers is: " + sum;



  1. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. JAVA Training in Chennai

  2. Thanks for sharing this valuable post to my knowledge great pleasure to be here SAS has great scope in IT industry. It’s an application suite that can change, manage & retrieve data from the variety of origin & perform statistical analytic on it…
    sas training in Chennai|sas course in Chennai|sas training institute in Chennai

  3. The information you have deliver here is really useful to make my knowledge good. Thanks for your heavenly post. It is truly supportive for us and I have accumulated some essential data from this blog.
    SAP training|SAP training in chennai|SAP Training in Chennai|sap training in Chennai

  4. Maharashtra Police Patil Recruitment 2016

    Hi, after reading this awesome article i am also happy to share my views here with colleagues ...............

  5. Great article.I learned some new concept. Thanks for sharing.
    Web designing training in chennai

  6. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Node JS training in chennai | Node JS training institute in chennai

  7. Updating with the latest technology and implementing it is the only way to survive in our niche. Thanks for making me this article. You have done a great job by sharing this content in here. Keep writing article like this.
    SAS Training in Chennai | SAS Training Institute in Chennai

  8. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Web Designing Training Institute in Chennai | DotNet Training Institute in Chennai | Java Training Institute in Chennai

  9. I like the post format as you create user engagement in the complete article. It seems round up of all published posts. Thanks for gauging the informative posts.
    cara menggugurkan kandungan
    http://jualobatpenggugurkandungan.net/ obat penggugur kandungan
    http://tandatandakehamilan.net/ tanda tanda kehamilan
    http://tandatandakehamilan.net/cara-cepat-dan-selamat-menggugurkan-kandungan/ cara menggugurkan kandungan
    http://obataborsi59.com/ obat aborsi
    http://obataborsi59.com/cara-menggugurkan-kandungan-dengan-cepat-dan-aman/ cara menggugurkan kandungan