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();
			myCallable.setNum(i);
			
			// 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);
			outputs.add(future.get());
		}

		for (String o : outputs) {
			System.out.println(o);
		}
		
		e.shutdown();
		
	}
}

// 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;
	}

}

9 comments:

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

    ReplyDelete
  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…
    Regards,
    sas training in Chennai|sas course in Chennai|sas training institute in Chennai

    ReplyDelete
  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.
    Regards,
    SAP training|SAP training in chennai|SAP Training in Chennai|sap training in Chennai

    ReplyDelete
  4. Maharashtra Police Patil Recruitment 2016


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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete