Sketch
In multithreading, sometimes we want a thread to return some values after execution. java.util.concurrent.Callable interface is introduced in java5, which is similar to the Runnable interface, but Callable can have return values.
The Java Callable interface uses the return type of commonly defined objects. The executor class provides useful methods for executing Java calls in the thread pool. Because callable tasks run in parallel, we must wait for the returned objects.
Future
The task to implement the Callable interface returns the java.util.concurrent.Future object. Using the Future object, we can get the return value
#Cancel task
#If the task has been completed or cancelled or cannot be cancelled for some reason, it will fail when the method is called
#If the task has not been started, the task will never execute after the method is called
#If the task has been started, if the parameter mayInterruptIfRunning is true, the task will be interrupted, otherwise the task execution will be allowed to complete
boolean cancel(boolean mayInterruptIfRunning);
#Judge whether the task has been cancelled
boolean isCancelled();
#Judge whether the task is completed
boolean isDone();
#Wait for the return value after the completion of the task execution
V get() throws InterruptedException, ExecutionException;
#Wait for the task to complete and get the return value within the specified time, otherwise a timeout exception will be thrown
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
Example
The following is a simple Callable call task example, which returns the name of the thread executing the task after one second. We use the Executor framework to execute 100 tasks in parallel, and use Java Future to get the results of the submitted tasks.
package com.lkf.mulithread;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(1000);
//Returns the current thread name
return Thread.currentThread().getName();
}
public static void main(String args[]){
//Use Executors to create a fixed size thread pool with the size of 10
ExecutorService executor = Executors.newFixedThreadPool(10);
//Create a list to hold Future objects related to Callable
List<Future<String>> list = new ArrayList<Future<String>>();
//Create a MyCallable instance
Callable<String> callable = new MyCallable();
for(int i=0; i< 100; i++){
//Submit task to thread pool
Future<String> future = executor.submit(callable);
//Put the return value into the collection
list.add(future);
}
for(Future<String> fut : list){
try {
//Print the return value. Note that there will be a delay, because Future.get() will wait for the task to complete
System.out.println(new Date()+ "::"+fut.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
//Close thread pool
executor.shutdown();
}
}
When we run the code above, you will find that the output will be delayed, because the Future get() method needs to wait for the task to complete. At the same time, we noticed that only 10 threads were performing tasks at a time.
Output results:
Sun Mar 25 14:09:50 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:51 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:52 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:53 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:54 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:55 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:56 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:57 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:58 CST 2018::pool-1-thread-7
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-6
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-2
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-9
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-4
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-5
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-8
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-10
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-1
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-3
Sun Mar 25 14:09:59 CST 2018::pool-1-thread-6
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-2
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-9
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-7
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-4
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-5
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-10
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-8
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-3
Sun Mar 25 14:10:00 CST 2018::pool-1-thread-1