概述
callable接口如下
@FunctionalInterface
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;
}
@FunctionalInterface表示函数式接口,说明这个接口只有一个抽象方法(也就是不是接口中的静态方法和默认方法要排除在外)。因为是函数式接口,所以也可以是如下使用
Runnable r = () -> System.out.println("hello world");
可以向线程池提交callable对象,并获取其返回值的future。
/**
* Submits a value-returning task for execution and returns a
* Future representing the pending results of the task. The
* Future's {@code get} method will return the task's result upon
* successful completion.
*
* <p>
* If you would like to immediately block waiting
* for a task, you can use constructions of the form
* {@code result = exec.submit(aCallable).get();}
*
* <p>Note: The {@link Executors} class includes a set of methods
* that can convert some other common closure-like objects,
* for example, {@link java.security.PrivilegedAction} to
* {@link Callable} form so they can be submitted.
*
* @param task the task to submit
* @param <T> the type of the task's result
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
<T> Future<T> submit(Callable<T> task);
使用
Callable任务返回Future对象。Callable和runnable的区别在于其有返回值并可以向上抛出异常。runnable线程产生的异常,要么在其内部解决,要么导致线程完蛋。注意,你不能利用Callable向线程传递参数。常规的办法是在构造Callable对象时进行传入。
package com.zyf.Future;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class FutureGetTimeOut1 {
public static void main(String[] args){
int timeout = 2;
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result = false;
Future<Boolean> future = executor.submit(new TaskThread("发送请求"));//将任务提交给线程池
try {
result = future.get(timeout, TimeUnit.SECONDS);
// result = future.get(timeout, TimeUnit.MILLISECONDS); //1
System.out.println("发送请求任务的返回结果: "+result); //2
} catch (InterruptedException e) {
System.out.println("线程中断出错。");
future.cancel(true);// 中断执行此任务的线程
} catch (ExecutionException e) {
System.out.println("线程服务出错。");
future.cancel(true);
} catch (TimeoutException e) {// 超时异常
System.out.println("超时。");
future.cancel(true);
}finally{
System.out.println("线程服务关闭。");
executor.shutdown();
}
}
static class TaskThread implements Callable<Boolean> {
private String t;
public TaskThread(String temp){
this.t= temp;
}
public Boolean call() {
//for用于模拟超时
for(int i=0;i<999999999;i++){
if(i==999999998){
System.out.println(t+"成功!");
}
if (Thread.interrupted()){ //很重要
return false;
}
}
System.out.println("继续执行..........");
return true;
}
}
}
还有一个非常实用的功能,就是为线程指定超时时间。Future get时可以指定等待的时间,超出等待时间会发出TimeoutException。这样,用户就可以在响应异常时取消调该线程(Future也提供取消的cancel)方法。
原理
Callable实际上底层也是用runable实现的。只不过是将返回结果存储在任务结果字段中。向Executors中提交Callable的任务时,首先会采用适配器模式进行转化。
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
//RunnableFuture接口继承了Runnable和Future两个接口
RunnableFuture<T> ftask = newTaskFor(task);
//使用execute提交普通的Runnable任务
execute(ftask);
return ftask;
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new FutureTask<T>(callable);
}
首先要了解核心类futureTask。他是一个可以包含future返回的runnable对象。
可以看到下面是包了一个callable对象和一个outcome结果。run里面调用call,并修改返回的值。包含异常。
public class FutureTask<V> implements RunnableFuture<V> {
/** The underlying callable; nulled out after running */
private Callable<V> callable;
/** The result to return or exception to throw from get() */
private Object outcome; // non-volatile, protected by state reads/writes
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();//结果设置到上面的outcome中
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
}
ScheduledThreadPoolExecutor的实际应用
在ScheduledThreadPoolExecutor中,覆盖了该方法
public <T> Future<T> submit(Callable<T> task) {
return schedule(task, 0, NANOSECONDS);
}
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay,
TimeUnit unit) {
if (callable == null || unit == null)
throw new NullPointerException();
RunnableScheduledFuture<V> t = decorateTask(callable,
new ScheduledFutureTask<V>(callable,
triggerTime(delay, unit)));
delayedExecute(t);
return t;
}
ScheduledFutureTask也是一个futureTask。
delayedExecute将任务加入执行队列。注意,ScheduledThreadPoolExecutor使用的是自己指定的DelayedWorkQueue
他的poll实现如下
public RunnableScheduledFuture<?> poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
RunnableScheduledFuture<?> first = queue[0];
if (first == null || first.getDelay(NANOSECONDS) > 0)
return null;
else
return finishPoll(first);
} finally {
lock.unlock();
}
}
其中
public long getDelay(TimeUnit unit) {
return unit.convert(time - now(), NANOSECONDS);
}