022 Future interface

I. Overview

In the previous section, we mentioned the Future object, which is an object that encapsulates the returned interface from the Callable interface.

  This section will talk about the Future interface.


 

2. Future interface 

public interface Future<V> {
  // cancel the thread task
    boolean cancel(boolean mayInterruptIfRunning);
    //whether
    boolean isCancelled(); 
  //whether the task is completed boolean isDone();   //Get operation, a blocking method V get () throws InterruptedException, ExecutionException; V get ( long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }

  This interface generally describes the object that returns the result, which is an asynchronous method. Blocking occurs only when the get() method is called.


 

Three summary

  We won't talk about the asynchronous problem here. This problem needs to be discussed later when thread pools and asynchronous tasks are used.

  Here you only need to know that Future can encapsulate the execution result of thread tasks.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325681639&siteId=291194637