Callable和Future的学习

Callable和Future的学习

一、Callable

Callable接口比起Runnable接口的优点就是可以获取线程运行的返回值。

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;
}

一般创建一个线程去运行需要实现Runnable,然后new Thread(Runnable),而Thread类没有以Callable接口为参的构造方法,那么该怎么运行实现了Callable接口的类呢?

二、Future和FutureTask
Future类源码:
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;
}

原文:http://www.cnblogs.com/dolphin0520/p/3949310.html
在Future接口中声明了5个方法,下面依次解释每个方法的作用:
1、cancel方法用来取消任务,如果取消任务成功则返回true,如果取消任务失败则返回false。参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务。如果任务已经完成,则无论mayInterruptIfRunning为true还是false,此方法肯定返回false,即如果取消已经完成的任务会返回false;如果任务正在执行,若mayInterruptIfRunning设置为true,则返回true,若mayInterruptIfRunning设置为false,则返回false;如果任务还没有执行,则无论mayInterruptIfRunning为true还是false,肯定返回true。
2、isCancelled方法表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回 true。
3、isDone方法表示任务是否已经完成,若任务完成,则返回true;
4、get()方法用来获取执行结果,这个方法会产生阻塞,会一直等到任务执行完毕才返回;
5、get(long timeout, TimeUnit unit)用来获取执行结果,如果在指定时间内,还没获取到结果,就直接返回null。  
也就是说Future提供了三种功能: 
 1)判断任务是否完成; 
 2)能够中断任务;  
 3)能够获取任务执行结果。  
 因为Future只是一个接口,所以是无法直接用来创建对象使用的,因此就有了下面的FutureTask。

FutureTask部分源码:
public class FutureTask<V> implements RunnableFuture<V>

FutureTask里面有两个构造方法,不过一般用第一个,至于为什么要有第二个构造方法,我认为是为了兼容以前的类,会把一个实现Runnable接口的类通过new RunnableAdapter<T>(Runnable task, T result)方法转化成Callable接口的类来继续运行。

public FutureTask(Callable<V> callable)
public FutureTask(Runnable runnable, V result){
        this.callable = Executors.callable(runnable, result);
        this.state = NEW;
}

而RunnableFuture接口又是继承自Runnable和Future:

public interface RunnableFuture<V> extends Runnable, Future<V>

所以FutureTask间接的实现了Future接口,并且为Future里面的5个抽象方法添加具体的实现。

三、Executors、Executor和ExecutorService
Executors:

一般用来创建指定的线程池:
1、创建指定线程数量的线程池:public static ExecutorService newFixedThreadPool(int n Threads)
2、创建只有一个线程的线程池:public static ExecutorService newSingleThreadExecutor()
3、创建可根据需要增加新线程的线程池:public static ExecutorService newCachedThreadPool()
4、创建一个可安排在给定延迟后运行命令或者定期地执行的线程池:public static ScheduledExecutorService newSingleThreadScheduledExecutor()
在这里插入图片描述

Executor:
public interface Executor {
    void execute(Runnable command);
}

execute方法用于在线程池中运行指定的实现Runnable接口的线程。

ExecutorService部分源码:
public interface ExecutorService extends Executor {
    void shutdown();
    
    List<Runnable> shutdownNow();

    boolean isShutdown();

    boolean isTerminated();

    boolean awaitTermination(long timeout, TimeUnit unit)

        throws InterruptedException;

    <T> Future<T> submit(Callable<T> task);

    <T> Future<T> submit(Runnable task, T result);

    Future<?> submit(Runnable task);
    }
实例:
package callable;



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 C_F_test {



     public static void main(String[] args) throws 
TimeoutException, InterruptedException, 
ExecutionException {

          ExecutorService threadPool = 
Executors.newCachedThreadPool();

          Future<Integer> future1 = threadPool.submit(new 
MyCallThread());

          Future<Integer> future2 = threadPool.submit(new 
MyCallThread());



          try {

              TimeUnit.SECONDS.sleep(2);

          } catch (InterruptedException e) {

              e.printStackTrace();

          }



          System.out.println("子线程1是否计算完成? : " + 
future1.isDone());

          

          //cancel方法最终调用interrupt()方法,而当future1线程在sleep的时候被interrupt时就会抛异常,

          //就会转向future1线程的异常处理模块

          System.out.println("是否打断了子线程1 : " + 
future1.cancel(true));

          

          System.out.println("子线程1是否计算完成? : " + 
future1.isDone());

          

          // 被取消后的线程是无法取到其运行结果的值的,因为这个线程在取消后已经没数据了。

          // System.out.println("当前计算的总和是 : " + 
future1.get());



          System.out.println("子线程2是否计算完成? : " + 
future2.isDone());

          System.out.println("子线程2的运行结果是 : " + 
future2.get());

          System.out.println("子线程2是否计算完成? : " + 
future2.isDone());



          threadPool.shutdownNow();

     }



}



class MyCallThread implements Callable<Integer> {



     @Override

     public Integer call() throws Exception {

          try {

              int i, sum = 0;

              for (i = 1; i <= 100; i++) {

                   sum += i;

                   TimeUnit.MILLISECONDS.sleep(100);

              }

              return sum;

          } catch (InterruptedException e) {

              //产生异常后当前线程就不是阻塞状态了,需要再次调用中断方法来中断当前线程(future1)

              Thread.currentThread().interrupt();

          }

          return 0;

     }



}


代码结果:

子线程1是否计算完成? : false

是否打断了子线程1 : true

子线程1是否计算完成? : true

子线程2是否计算完成? : false

子线程2的运行结果是 : 5050

子线程2是否计算完成? : true

如果在MyCallThread类里的catch块中没写Thread.currentThread().interrupt();就会抛异常,如下:

在这里插入图片描述

原因我在代码里写了:FutureTask类的cancel()方法最终会调用 interrupt方法,

  if (mayInterruptIfRunning) {

                try {

                    Thread t = runner;

                    if (t != null)

                        t.interrupt();

                } finally { // final state

                    UNSAFE.putOrderedInt(this, stateOffset, 
INTERRUPTED);

                }

而这时future1线程有很大可能在sleep,一旦被打断就会进入catch块处理,当前线程就会由阻塞状态(sleep)变成非阻塞状态(因为进入catch了,不在sleep了),
需要在catch块手动中断当前线程。


参考:
1、http://www.cnblogs.com/dolphin0520/p/3949310.html
2、https://blog.csdn.net/Androidlushangderen/article/details/54984681
3、https://blog.csdn.net/srzhz/article/details/6804756
4、https://blog.csdn.net/ghsau/article/details/7443324

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值