Table of Contents
Use of Callable
Future
declares 5 methods in the Future interface. The function of each method is explained below.
Future provides three functions:
FutureTask
FutureTask implements the RunnableFuture interface. The definition of this interface is as follows:
FutureTask的例子
Home Java javaTutorial Detailed introduction to Callable and Future in Java multithreading (code example)

Detailed introduction to Callable and Future in Java multithreading (code example)

Feb 11, 2019 am 11:01 AM
java Multithreading

This article brings you a detailed introduction (code example) about Callable and Future in Java multi-threading. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The reason why Callable and Future appear

There are two ways to create a thread, one is to directly inherit Thread, and the other is to implement the Runnable interface.
These two methods have a flaw: the execution results cannot be obtained after completing the task.
If you need to obtain the execution results, you must achieve the effect through shared variables or thread communication, which is more troublesome to use.
Since Java 1.5, Callable and Future have been provided, through which the task execution results can be obtained after the task execution is completed.

Introduction to Callable and Future

The Callable interface represents a piece of code that can be called and returns a result; the Future interface represents an asynchronous task, which is the future given by the task that has not yet been completed. result. So Callable is used to generate results, and Future is used to obtain results.
The Callable interface uses generics to define its return type. The Executors class provides some useful methods to execute tasks within the Callable in the thread pool. Since the Callable task is parallel (parallel means that the whole thing looks parallel, in fact, only one thread is executing at a certain point in time), we must wait for the result it returns.
java.util.concurrent.Future object solves this problem for us. After the thread pool submits the Callable task, a Future object is returned. You can use it to know the status of the Callable task and get the execution result returned by the Callable. Future provides the get() method so that we can wait for the Callable to end and obtain its execution results.

Callable and Runnable

java.lang.Runnable, it is an interface, and only one run() method is declared in it:

public interface Runnable {
    public abstract void run();
}
Copy after login

Since the return value of the run() method is void type, no results can be returned after the task is executed.

Callable is located under the java.util.concurrent package. It is also an interface, and only one method is declared in it, but this method is called call():

public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}
Copy after login

This is a For generic interfaces, the type returned by the call() function is the V type passed in.

Use of Callable

Generally, it is used in conjunction with ExecutorService. Several overloaded versions of the submit method are declared in the ExecutorService interface.

<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
Copy after login

The parameter type in the first submit method is Callable.

For the time being, you only need to know that Callable is generally used in conjunction with ExecutorService. The specific usage method will be described later.

Generally, we use the first submit method and the third submit method, and the second submit method is rarely used.

Future

Future is to cancel the execution result of a specific Runnable or Callable task, query whether it is completed, and obtain the result. If necessary, you can obtain the execution result through the get method, which will block until the task returns the result.

The Future class is located under the java.util.concurrent package. It is an interface:

public interface Future<V> {
    boolean cancel(boolean mayInterruptIfRunning);
    boolean isCancelled();
    boolean isDone();
    V get() throws InterruptedException, ExecutionException;
    V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}
Copy after login
declares 5 methods in the Future interface. The function of each method is explained below.
  • The cancel method is used to cancel the task. If the task cancellation is successful, it returns true. If the task cancellation fails, it returns false. The parameter mayInterruptIfRunning indicates whether it is allowed to cancel tasks that are being executed but have not been completed. If set to true, it means that tasks in the process of execution can be canceled. If the task has been completed, whether mayInterruptIfRunning is true or false, this method will definitely return false, that is, if the completed task is canceled, it will return false; if the task is being executed, if mayInterruptIfRunning is set to true, it will return true, if mayInterruptIfRunning is set to false , then return false; if the task has not been executed, then whether mayInterruptIfRunning is true or false, it will definitely return true.

  • The isCancelled method indicates whether the task was successfully canceled. If the task is successfully canceled before the task is completed normally, it returns true.

  • isDone method indicates whether the task has been completed. If the task is completed, it returns true;

  • get() method is used to obtain the execution result. This method will block and will wait until the task execution is completed before returning;

  • get(long timeout, TimeUnit unit) is used to obtain the execution result. If it has not been obtained within the specified time, When the result is reached, null is returned directly.

Future provides three functions:
  1. to determine whether the task is completed;

  2. to be able to interrupt Task;

  3. can obtain the task execution results.

    Because Future is just an interface, it cannot be used directly to create objects, so there is the following FutureTask.

    FutureTask

    FutureTask implements the RunnableFuture interface. The definition of this interface is as follows:
public interface RunnableFuture<V> extends Runnable, Future<V> {  
    void run();  
}
Copy after login

You can see that this interface implements the Runnable and Future interfaces. The specific implementation in the interface is implemented by FutureTask. The two construction methods of this class are as follows:

public FutureTask(Callable<V> callable) {  
        if (callable == null)  
            throw new NullPointerException();  
        sync = new Sync(callable);  
    }  
    public FutureTask(Runnable runnable, V result) {  
        sync = new Sync(Executors.callable(runnable, result));  
    }
Copy after login

如上提供了两个构造函数,一个以Callable为参数,另外一个以Runnable为参数。这些类之间的关联对于任务建模的办法非常灵活,允许你基于FutureTask的Runnable特性(因为它实现了Runnable接口),把任务写成Callable,然后封装进一个由执行者调度并在必要时可以取消的FutureTask。

FutureTask可以由执行者调度,这一点很关键。它对外提供的方法基本上就是Future和Runnable接口的组合:get()、cancel、isDone()、isCancelled()和run(),而run()方法通常都是由执行者调用,我们基本上不需要直接调用它。

FutureTask的例子
public class MyCallable implements Callable<String> {  
    private long waitTime;   
    public MyCallable(int timeInMillis){   
        this.waitTime=timeInMillis;  
    }  
    @Override  
    public String call() throws Exception {  
        Thread.sleep(waitTime);  
        //return the thread name executing this callable task  
        return Thread.currentThread().getName();  
    }  

}
Copy after login
public class FutureTaskExample {  
     public static void main(String[] args) {  
        MyCallable callable1 = new MyCallable(1000);                       // 要执行的任务  
        MyCallable callable2 = new MyCallable(2000);  

        FutureTask<String> futureTask1 = new FutureTask<String>(callable1);// 将Callable写的任务封装到一个由执行者调度的FutureTask对象  
        FutureTask<String> futureTask2 = new FutureTask<String>(callable2);  

        ExecutorService executor = Executors.newFixedThreadPool(2);        // 创建线程池并返回ExecutorService实例  
        executor.execute(futureTask1);  // 执行任务  
        executor.execute(futureTask2);    

        while (true) {  
            try {  
                if(futureTask1.isDone() && futureTask2.isDone()){//  两个任务都完成  
                    System.out.println("Done");  
                    executor.shutdown();                          // 关闭线程池和服务   
                    return;  
                }  

                if(!futureTask1.isDone()){ // 任务1没有完成,会等待,直到任务完成  
                    System.out.println("FutureTask1 output="+futureTask1.get());  
                }  

                System.out.println("Waiting for FutureTask2 to complete");  
                String s = futureTask2.get(200L, TimeUnit.MILLISECONDS);  
                if(s !=null){  
                    System.out.println("FutureTask2 output="+s);  
                }  
            } catch (InterruptedException | ExecutionException e) {  
                e.printStackTrace();  
            }catch(TimeoutException e){  
                //do nothing  
            }  
        }  
    }  
}
Copy after login

The above is the detailed content of Detailed introduction to Callable and Future in Java multithreading (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles