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

  10. Enjoyed reading the article above , really explains everything in
    detail,the article is very interesting and effective.Thank you and good
    luck for the upcoming articles
    clipping path
    clipping path service
    background removal
    raster to vector

  11. Looking For RRB ALP Merit List 2018? Look no more. Here, we are, providing the all RRB Merit List, may it be Group D, ALP, and RPF right, at one place at railwayresult.in. So, keep yourself updated with RRB Merit List.

  12. Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
    German Classes in Chennai
    German Language Classes in Chennai
    Angularjs Training in Chennai
    Selenium Training in Chennai
    Software Testing Training in Chennai
    German classes in Anna Nagar
    German classes in Velachery

  13. Hey...thank you..this is the great site...you can check all India Govt & Private Notification updates....check all University Time table..
    TMBU Time Tale 2020
    Kota University Time Table 2020
    MDSU Time Table 2020
    MGSU Time Table 2020
    MLSU Time Table 2020

  14. Excellent article. It is Very interesting to read. I really love to read such a nice article. ...FCI Recruitment 2020 Vision and Mission for this corporation is to give food grains for a reasonable price and produce an efficient product and storage the food grains for distribution to the public..