Tutorial

Java Callable Future Example

Published on August 3, 2022
author

Pankaj

Java Callable Future Example

Java Callable and Future are used a lot in multithreaded programming. In last few posts, we learned a lot about java threads but sometimes we wish that a thread could return some value that we can use. Java 5 introduced java.util.concurrent.Callable interface in concurrency package that is similar to Runnable interface but it can return any Object and able to throw Exception.

Java Callable

java Callable, java Future, java callable example, java executorservice callable Java Callable interface use Generic to define the return type of Object. Executors class provide useful methods to execute Java Callable in a thread pool. Since callable tasks run in parallel, we have to wait for the returned Object.

Java Future

Java Callable tasks return java.util.concurrent.Future object. Using Java Future object, we can find out the status of the Callable task and get the returned Object. It provides get() method that can wait for the Callable to finish and then return the result. Java Future provides cancel() method to cancel the associated Callable task. There is an overloaded version of get() method where we can specify the time to wait for the result, it’s useful to avoid current thread getting blocked for longer time. There are isDone() and isCancelled() methods to find out the current status of associated Callable task. Here is a simple example of Java Callable task that returns the name of thread executing the task after one second. We are using Executor framework to execute 100 tasks in parallel and use Java Future to get the result of the submitted tasks.

package com.journaldev.threads;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        Thread.sleep(1000);
        //return the thread name executing this callable task
        return Thread.currentThread().getName();
    }
    
    public static void main(String args[]){
        //Get ExecutorService from Executors utility class, thread pool size is 10
        ExecutorService executor = Executors.newFixedThreadPool(10);
        //create a list to hold the Future object associated with Callable
        List<Future<String>> list = new ArrayList<Future<String>>();
        //Create MyCallable instance
        Callable<String> callable = new MyCallable();
        for(int i=0; i< 100; i++){
            //submit Callable tasks to be executed by thread pool
            Future<String> future = executor.submit(callable);
            //add Future to the list, we can get return value using Future
            list.add(future);
        }
        for(Future<String> fut : list){
            try {
                //print the return value of Future, notice the output delay in console
                // because Future.get() waits for task to get completed
                System.out.println(new Date()+ "::"+fut.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        //shut down the executor service now
        executor.shutdown();
    }

}

Once we execute the above program, you will notice the delay in output because java Future get() method waits for the java callable task to complete. Also notice that there are only 10 threads executing these tasks. Here is snippet of the output of above program.

Mon Dec 31 20:40:15 PST 2012::pool-1-thread-1
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-2
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-3
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-4
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-5
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-6
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-7
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-8
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-9
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-10
Mon Dec 31 20:40:16 PST 2012::pool-1-thread-2
...

Tip: What if we want to override some of the methods of Java Future interface, for example overriding get() method to timeout after some default time rather than waiting indefinitely, in this case Java FutureTask class comes handy that is the base implementation of Future interface. Check out Java FutureTask Example to learn more about this class.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 30, 2013

Hi, I believe that your implementation is not the best you can do in this scenario. Your callable waits for exactly 1 second, but if there was a little bit of random, you will probably notice that the get() method, a blocking one, will slow down the execution in the for loop(because some of the results are not ready yet). Rewrite it using a while(!list.isEmpty()) block, every time you have a Future ready(use isDone()) you can safely get the result without delaying the execution and remove it from the list. HTH

- Silviu Burcea

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 30, 2013

Hi Silviu, This is just an example to show how can we use it, in real life scenarios most of the times we will start a callable and then we get the response later on. Obviously we can modify for better performance but if I will add too much implementation, we will loose focus on learning the Callable-Future usage. Thanks, Pankaj

- Pankaj

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 19, 2015

    If we use while(!list.isEmpty()) { future.iosDone() … }, don’t we consume waste CPU cycles checking for isDone continuously ?

    - Rvindranath

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      November 26, 2019

      Hi, I see you says,I feel you are right,but how can reslove the cpu waste question

      - ants_double

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        September 6, 2013

        All the tutorial are very Good and helpful. I want to run more than 1000 threads (Tasks) but not at a time may be 20 at a time using one of thread pools But I also want to cancel the task if it is blocked for more than 30 minutes. But that 30 minutes are not including submit times of the task. No more than 30 minutes should be used for execution. Is there any function for any class using that can put the maximum execution time? If yes will that function interrupt the thread that is executing the task? If yes then do we need to handle “ InterruptedException” for that thread? Please give your best suggestion and thanks for this good articles

        - Agha Khan

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          April 9, 2014

          Hi Pankaj, We normally pass a “User/Worker Thread instance” to the ThreadPoolExecutor/Scheduler but here the Callable Interface is not extending Runnable interface neither we are overriding any run() method. We are creating 100 callable instance but not thread, does it mean we cannot use callable for multi-threading purpose

          - HIMANSU NAYAK

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          June 3, 2014

          We are creating only 1 instance of Callable implementation. When we submit it to the Executor, it creates the thread for us and execute it. This is multithreading.

          - Pankaj

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            April 18, 2015

            e.execute®; is the same that… (new Thread(new Runnable() {…})).start();

            - X

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              June 3, 2014

              Hi Pankaj. One doubt regarding Callable interface. The compiler creates a synthetic method to invoke the call() method. Why is it required?

              - Sayantan

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              June 3, 2014

              I didn’t understood what you mean here?

              - Pankaj

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                September 15, 2014

                You are the best and my favorite in Threading concepts!!

                - Praveen

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  October 21, 2014

                  Nice examples you have given pankaj. Great Job :) Thanks.

                  - Hemanth

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    May 27, 2015

                    Thanks, nice post

                    - Binh Thanh Nguyen

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      May 31, 2015

                      Hi Pankaj, In my project we have one batch running more time. The scenario is one record getting from one database and inserting into another database. It is happening record by record. I want to execute these parallarly. In this case can I implement collable?

                      - ramesh

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      April 4, 2017

                      Yes You can do the same by adding an extra flag. Set the flag value when any of your thread pick the row to insert.

                      - Vishal

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        July 22, 2015

                        thanks a lot.

                        - laudukang

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          October 8, 2015

                          First of all, THANK YOU! A well written article and you explained the concepts so clearly. The code examples for marvelous! When is your book coming out? ;-) Russ

                          - Russ Ray

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          October 9, 2015

                          Thanks for the appreciation Russ, there are few eBooks I have written that you can download after subscribing to newsletter.

                          - Pankaj

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            October 26, 2015

                            Hi…it is really easy explanation .thanks for that…do you have any good material in angularjs?

                            - aditi

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              November 18, 2015

                              Super note. Thanks for sharing the stuff

                              - Chaitanya

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                December 7, 2015

                                Thanks ! , useful…!

                                - shgy

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  October 24, 2016

                                  Hi Pankaj, This is very useful. Thanks fro sharing your knowledge with us.

                                  - Veranga Sooriyabandara

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    July 10, 2017

                                    Thanks for sharing simple and understandable example. I have a question, it may be very silly but i want to clarify it with you. In above example we will always get output as : pool-1-thread-(Number), where pool-1 is common, here my question is since we have created pool of size 5. Output should also change accordingly like pool-1, pool-2 etc.

                                    - Utpal

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      October 13, 2017

                                      Thanks for this tutorial! I did have to make one modification. I changed (A) to (B) where: (A) Future future = executor.submit(callable); (B) Future future = executor.submit(new MyCallable());

                                      - Shane

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        February 15, 2018

                                        Thanks for the example.

                                        - Aakshi

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          August 21, 2018

                                          hi, thanks for nice explanation of threadpool. I have some question. please clear my understanding. 1.Here the thread pool size is 10. So it creates 10 threads right? that means whatever the size we give to executor creates that many threads and all threads execute same method call() . 2.Here in output why we have output for thred-2 again as per method once it prints method finish then why its executing thread again ?

                                          - urvi

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            September 28, 2018

                                            Place Callable callable = new MyCallable(); into “fori” It will be right

                                            - Alex

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              March 9, 2019

                                              Thanks a lot,it really helped me :)

                                              - BGRRRRR

                                                Try DigitalOcean for free

                                                Click below to sign up and get $200 of credit to try our products over 60 days!

                                                Sign up

                                                Join the Tech Talk
                                                Success! Thank you! Please check your email for further details.

                                                Please complete your information!

                                                Become a contributor for community

                                                Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                DigitalOcean Documentation

                                                Full documentation for every DigitalOcean product.

                                                Resources for startups and SMBs

                                                The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                Get our newsletter

                                                Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                New accounts only. By submitting your email you agree to our Privacy Policy

                                                The developer cloud

                                                Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                Get started for free

                                                Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                *This promotional offer applies to new accounts only.