Details and examples of javaCallable and Future

  • 2020-05-30 20:18:07
  • OfStack

java Callable Future

Callable and Future were added in later versions of Java in order to adapt to the multithreading method. Callable is an interface similar to Runnable. The class that implements Callable interface and the class that implements Runnable are tasks that can be performed by other threads.

The interface of Callable is defined as follows;

public interface Callable<V> { 

   V  call()  throws Exception; 
<span id="transmark"></span>

The difference between Callable and Runnable is as follows:

The method I Callable defines is call, and the method Runnable defines is run.

The call method of II Callable can have a return value, while the run method of Runnable cannot.

The call method of III Callable can throw an exception, while the run method of Runnable cannot.

Future introduction

Future represents the result of an asynchronous calculation, which provides a way to check if the calculation has completed, wait for the calculation to complete, and retrieve the result of the calculation. The cancel method of Future can cancel the execution of a task. It has a Boolean parameter of true, which means to interrupt the execution of the task immediately, and false, which means to allow the running task to complete. The get method of Future waits for the calculation to be completed to obtain the calculation result

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;


 * Callable  and  Future interface 

 * Callable Is similar to the Runnable Interface, implementation Callable The class and implementation of the interface Runnable Are tasks that can be performed by other threads. 

 * Callable and Runnable There are several differences: 

 *  ( 1 ) Callable The prescribed method is call() And the Runnable The prescribed method is run().

 *  ( 2 ) Callable The task can return a value after execution, and Runnable Cannot return a value. 

 *  ( 3 ) call() Method can throw an exception, and run() A method cannot throw an exception. 

 *  ( 4 Run) Callable Mission available 1 a Future Object, 

 * Future  Represents the result of an asynchronous calculation. It provides a way to check that the computation is complete, to wait for the computation to complete, and to retrieve the result of the computation. 

 *  through Future Object can understand the execution of a task, cancel the execution of a task, and get the result of the execution of a task. 


public class CallableAndFuture {

  public static class MyCallable implements Callable{

     private int flag = 0; 

     public MyCallable(int flag){

         this.flag = flag;


     public String call() throws Exception{

       if (this.flag == 0){ 

           return "flag = 0";


      if (this.flag == 1){  

        try {

          while (true) {




        } catch (InterruptedException e) {



        return "false";

      } else {  

            throw new Exception("Bad flag value!");




  public static void main(String[] args) {

    //  define 3 a Callable Type of task 

    MyCallable task1 = new MyCallable(0);

    MyCallable task2 = new MyCallable(1);

    MyCallable task3 = new MyCallable(2);


    //  create 1 A service that performs tasks 

    ExecutorService es = Executors.newFixedThreadPool(3);

    try {

      //  Commit and execute the task, and the task returns when it starts 1 a Future Object, 

      //  You can do this if you want the result of a task execution or if you want an exception Future Object to manipulate 

      Future future1 = es.submit(task1);

      //  For the first 1 The result of a task, if called get Method, the current thread waits for the task to complete before proceeding 

      System.out.println("task1: " + future1.get());


      Future future2 = es.submit(task2);

      //  Waiting for the 5 Seconds later, stop again 2 A task. Because the first 2 The tasks are in an infinite loop 


      System.out.println("task2 cancel: " + future2.cancel(true));


      //  For the first 3 The output of the task as performed by the 3 Four tasks will cause an exception 

      //  So the following statement will cause an exception to be thrown 

      Future future3 = es.submit(task3);

      System.out.println("task3: " + future3.get());

    } catch (Exception e){



    //  Stop the task execution service 




Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: