Callable和Runnable
先看一下两个接口的定义:

Callable

public interface Callable<V> {

  V call() throws Exception;

}


Runnable

interface Runnable {

  public abstract void run();

}

和明显能看到区别:

1.Callable能接受一个泛型,然后在call方法中返回一个这个类型的值。而Runnable的run方法没有返回值
2.Callable的call方法可以抛出异常,而Runnable的run方法不会抛出异常。


Future
返回值Future也是一个接口,通过他可以获得任务执行的返回值。

public interface Future<V> {

  boolean cancel(boolean var1);

  boolean isCancelled();

  boolean isDone();

  V get() throws InterruptedException, ExecutionException;

  V get(long var1, TimeUnit var3) throws InterruptedException, ExecutionException, TimeoutException;

}


其中的get方法获取的就是返回值。

来个例子

public class Main {
  public static void main(String[] args) throws InterruptedException, ExecutionException {
  ExecutorService executor = Executors.newFixedThreadPool(2);
  //创建一个Callable,3秒后返回String类型
  Callable myCallable = new Callable() {
    @Override
    public String call() throws Exception {
      Thread.sleep(3000);
      System.out.println("calld方法执行了");
      return "call方法返回值";
    }
  };
  System.out.println("提交任务之前 "+getStringDate());
  Future future = executor.submit(myCallable);
  System.out.println("提交任务之后,获取结果之前 "+getStringDate());
  System.out.println("获取返回值: "+future.get());
  System.out.println("获取到结果之后 "+getStringDate());
  }
  public static String getStringDate() {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    String dateString = formatter.format(currentTime);
    return dateString;
    }
  }


通过executor.submit提交一个Callable,返回一个Future,然后通过这个Future的get方法取得返回值。

看一下输出:

提交任务之前 12:13:01
提交任务之后,获取结果之前 12:13:01
calld方法执行了
获取返回值: call方法返回值
获取到结果之后 12:13:04


get()方法的阻塞性
通过上面的输出可以看到,在调用submit提交任务之后,主线程本来是继续运行了。但是运行到future.get()的时候就阻塞住了,一直等到任务执行完毕,拿到了返回的返回值,主线程才会继续运行。

这里注意一下,他的阻塞性是因为调用get()方法时,任务还没有执行完,所以会一直等到任务完成,形成了阻塞。

任务是在调用submit方法时就开始执行了,如果在调用get()方法时,任务已经执行完毕,那么就不会造成阻塞。


参考资料:

https://www.cnblogs.com/syp172654682/p/9788051.html