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(); }
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; }
This is a For generic interfaces, the type returned by the call() function is the V type passed in.
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);
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 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; }
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.
to determine whether the task is completed;
to be able to interrupt Task;
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.
public interface RunnableFuture<V> extends Runnable, Future<V> { void run(); }
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)); }
如上提供了两个构造函数,一个以Callable为参数,另外一个以Runnable为参数。这些类之间的关联对于任务建模的办法非常灵活,允许你基于FutureTask的Runnable特性(因为它实现了Runnable接口),把任务写成Callable,然后封装进一个由执行者调度并在必要时可以取消的FutureTask。
FutureTask可以由执行者调度,这一点很关键。它对外提供的方法基本上就是Future和Runnable接口的组合:get()、cancel、isDone()、isCancelled()和run(),而run()方法通常都是由执行者调用,我们基本上不需要直接调用它。
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(); } }
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 } } } }
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!