Callable与Runnable的区别及其在JDK源码中的应用

来源:互联网 发布:css和php哪个好 编辑:程序博客网 时间:2024/05/16 05:20

  最近在学习Java多线程方面的东西,在此希望把自己学到的东西做做总结,要想搞清楚实现原理,源码是最好的老师,因此这篇我打算从实践+源码角度来进行分析以下几个问题:

        (1):Callable与Runnable的区别;

        (2):Callable与Runnable的使用,并且通过Future对象获取Callable的返回值; 

        (3):JDK源码中对于Callable与Runnable是怎么使用的呢?

        首先我们来看看源码中是怎么解释Callable和Runnable区别的:

        Callable与Runnable的区别:

[java] view plain copy
  1. * <p>The <tt>Callable</tt> interface is similar to {@link  
  2. * java.lang.Runnable}, in that both are designed for classes whose  
  3. * instances are potentially executed by another thread.  A  
  4. * <tt>Runnable</tt>, however, does not return a result and cannot  
  5. throw a checked exception.  
        从注释中可以看出Callable与Runnable的区别在于:

        (1):Callcble是可以有返回值的,具体的返回值就是在Callable的接口方法call返回的,并且这个返回值具体是通过实现Future接口的对象的get方法获取的,这个方法是会造成线程阻塞的;而Runnable是没有返回值的,因为Runnable接口中的run方法是没有返回值的;

        (2):Callable里面的call方法是可以抛出异常的,我们可以捕获异常进行处理;但是Runnable里面的run方法是不可以抛出异常的,异常要在run方法内部必须得到处理,不能向外界抛出;

        Callable与Runnable的使用实例,同时使用Future对象获取Callable中call方法的返回值:

        对于Callable的使用,有两种方式可以实现:

        方式1:通过单线程的方式我们自己实现,首先创建一个FutureTask对象,在创建FutureTask对象的时候,需要传入一个实现了Callable接口的对象,接着以该FutureTask作为Thread的参数,调用Thread的start方法开启线程执行任务,最后使用FutureTask的get方法获取到任务的返回值就可以了;那么为什么FutureTask对象可以作为Thread的参数呢?原因就在于FutureTask类实现了RunnableFuture接口,而RunnableFuture接口实现了Runnable和Future接口,那么当然可以作为Thread的参数了,我们来看看这种实现方式;

[java] view plain copy
  1. public class CallableTest {  
  2.     public static void main(String[] args) {  
  3.         //创建实现了Callable接口的对象  
  4.         MyCallable callable = new MyCallable();  
  5.         //将实现Callable接口的对象作为参数创建一个FutureTask对象  
  6.         FutureTask<String> task = new FutureTask<>(callable);  
  7.         //创建线程处理当前callable任务  
  8.         Thread thread = new Thread(task);  
  9.         //开启线程  
  10.         System.out.println("开始执行任务的时间: "+getNowTime());  
  11.         thread.start();  
  12.         //获取到call方法的返回值  
  13.         try {  
  14.             String result = task.get();  
  15.             System.out.println("得到返回值: "+result);  
  16.             System.out.println("结束执行get的时间: "+getNowTime());  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.       
  22.     public static String getNowTime()  
  23.     {  
  24.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  25.         return format.format(new Date());  
  26.     }  
  27. }  
  28. class MyCallable implements Callable<String>  
  29. {  
  30.     @Override  
  31.     public String call() throws Exception {  
  32.         Thread.sleep(3000);  
  33.         return "call method result";  
  34.     }  
  35. }  
        查看输出:

[java] view plain copy
  1. 开始执行任务的时间: 2016-08-31 12:21:43  
  2. 得到返回值: call method result  
  3. 结束执行get的时间: 2016-08-31 12:21:46  
        可以看到确实得到了call方法的返回值,但是在调用get方法的时候却造成了主线程的阻塞,因为我们在call方法里面让子线程暂停了3秒,这时候如果不阻塞主线程的话,输出语句中的第三行时间不应该是12:21:46的,应该是12:21:43,因此验证了上面我们给出的结论,但是使用Runnable接口是不会造成主线程阻塞的,具体实例马上给出;

        方式2:采用线程池的方式,我们可以使用线程池的方式来将当前实现Callable任务通过ThreadPoolExecutor的submit方法添加到线程池,submit方法会返回一个FutureTask对象,而FutureTask是实现了Future接口的,我们可以使用submit返回的FutureTask对象的get方法获取到任务的返回值,其实等会在源码分析的过程中你会发现线程池实现方式只是对我们自己采用线程方式实现的一种封装而已,没什么特别的啦;

[java] view plain copy
  1. public class CallableTest {  
  2.     public static void main(String[] args) {  
  3.         //创建实现了Callable接口的对象  
  4.         MyCallable callable = new MyCallable();  
  5.         //创建用于处理任务的线程池  
  6.         ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);   
  7.         //将任务添加到线程池中并且获得返回的FutureTask对象  
  8.         System.out.println("提交任务的时间: "+getNowTime());  
  9.         FutureTask<String> task = (FutureTask<String>) threadPool.submit(callable);  
  10.         //获取到call方法的返回值  
  11.         try {  
  12.             String result = task.get();  
  13.             System.out.println("得到返回值: "+result);  
  14.             System.out.println("结束执行get的时间: "+getNowTime());  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
  19.       
  20.     public static String getNowTime()  
  21.     {  
  22.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  23.         return format.format(new Date());  
  24.     }  
  25. }  
  26. class MyCallable implements Callable<String>  
  27. {  
  28.     @Override  
  29.     public String call() throws Exception {  
  30.         Thread.sleep(3000);  
  31.         return "call method result";  
  32.     }  
  33. }  
        查看输出:

[java] view plain copy
  1. 提交任务的时间: 2016-08-31 14:44:09  
  2. 得到返回值: call method result  
  3. 结束执行get的时间: 2016-08-31 14:44:12  
        可以看到,使用线程池方式和我们自己定义线程实现效果是一样的,这就是Callable使用的两种方式啦;如果你对线程池的实现原理不是很清楚的话,可以查看我的另一篇博客:我眼中的java线程池实现原理;

        使用Runnable处理任务的情况:

[java] view plain copy
  1. public class RunnableTest {  
  2.     public static void main(String[] args) {  
  3.         MyRunnable runnable = new MyRunnable();  
  4.         Thread thread = new Thread(runnable);  
  5.         System.out.println("开始执行任务时间:  "+getNowTime());  
  6.         thread.start();  
  7.         System.out.println("启动任务之后时间:  "+getNowTime());  
  8.     }  
  9.       
  10.     public static String getNowTime()  
  11.     {  
  12.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  13.         return format.format(new Date());  
  14.     }  
  15. }  
  16. class MyRunnable implements Runnable  
  17. {  
  18.     @Override  
  19.     public void run() {  
  20.         try {  
  21.             Thread.sleep(3000);  
  22.         } catch (InterruptedException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26. }  
        查看输出:

[java] view plain copy
  1. 开始执行任务时间:  2016-08-31 14:36:01  
  2. 启动任务之后时间:  2016-08-31 14:36:01  
        可以看到,两次时间是一致的,说明执行的runnable任务并没有影响主线程任务的执行;

        至此,我们有三个疑问需要解答,(1):使用Callable的过程中是怎样通过Future对象来获取返回值的,虽然从方法上讲调用Future的get方法就可以了,但是这个get方法里面的值是怎么来的;(2):为什么Future的get方法会带来阻塞问题;(3):采用线程池的方式处理Callable任务,JDK为我们封装了什么?下面从源码角度一一解答:

        首先来看第一和第二个问题:

        我们以自己定义线程使用Callable为例,在调用了线程的start方法之后会使得该线程处于就绪状态,有了竞争CPU时间片的权限,当分配到时间片之后,就会执行创建他的参数的run方法,也就是FutureTask的run方法,查看FutureTask的run方法如下:

[java] view plain copy
  1. public void run() {  
  2.         if (state != NEW ||  
  3.             !UNSAFE.compareAndSwapObject(this, runnerOffset,  
  4.                                          null, Thread.currentThread()))  
  5.             return;  
  6.         try {  
  7.             Callable<V> c = callable;  
  8.             if (c != null && state == NEW) {  
  9.                 V result;  
  10.                 boolean ran;  
  11.                 try {  
  12.                     result = c.call();  
  13.                     ran = true;  
  14.                 } catch (Throwable ex) {  
  15.                     result = null;  
  16.                     ran = false;  
  17.                     setException(ex);  
  18.                 }  
  19.                 if (ran)  
  20.                     set(result);  
  21.             }  
  22.         } finally {  
  23.             // runner must be non-null until state is settled to  
  24.             // prevent concurrent calls to run()  
  25.             runner = null;  
  26.             // state must be re-read after nulling runner to prevent  
  27.             // leaked interrupts  
  28.             int s = state;  
  29.             if (s >= INTERRUPTING)  
  30.                 handlePossibleCancellationInterrupt(s);  
  31.         }  
  32.     }  

        第12行执行了c的call方法,而c就是我们在创建FutureTask对象的时候传递进来的实现了Callable接口的对象,也就是执行了我们任务的逻辑操作了,将返回的结果赋值给了result,并且在不发生异常的情况下会执行第20行的set方法,来看set方法:

[java] view plain copy
  1. protected void set(V v) {  
  2.        if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {  
  3.            outcome = v;  
  4.            UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state  
  5.            finishCompletion();  
  6.        }  
  7.    }  
        set方法做的工作比较简单,就是将结果赋给了outcome而已,outcome是Object类型的全局变量,同时将state状态设置为NORMAL,接着会执行finishCompletion,这个方法其实就是用来唤醒我们上面想要获得数据的get方法的,我们先来看看get方法里面的实现:

[java] view plain copy
  1. public V get() throws InterruptedException, ExecutionException {  
  2.      int s = state;  
  3.      if (s <= COMPLETING)  
  4.          s = awaitDone(false, 0L);  
  5.      return report(s);  
  6.  }  
        可以看到首先是先去查看当前state状态是否小于等于COMPLETING,你查看FutureTask源码的话,会发现有这么几种状态:

[java] view plain copy
  1. private static final int NEW          = 0;  
  2. private static final int COMPLETING   = 1;  
  3. private static final int NORMAL       = 2;  
  4. private static final int EXCEPTIONAL  = 3;  
  5. private static final int CANCELLED    = 4;  
  6. private static final int INTERRUPTING = 5;  
  7. private static final int INTERRUPTED  = 6;  
       state状态小于等于COMPLETING就表示我们刚刚的Callable是还没有处理完成的,那么就会调用awaitDone方法:

[java] view plain copy
  1. private int awaitDone(boolean timed, long nanos)  
  2.         throws InterruptedException {  
  3.         final long deadline = timed ? System.nanoTime() + nanos : 0L;  
  4.         WaitNode q = null;  
  5.         boolean queued = false;  
  6.         for (;;) {  
  7.             if (Thread.interrupted()) {  
  8.                 removeWaiter(q);  
  9.                 throw new InterruptedException();  
  10.             }  
  11.   
  12.             int s = state;  
  13.             if (s > COMPLETING) {  
  14.                 if (q != null)  
  15.                     q.thread = null;  
  16.                 return s;  
  17.             }  
  18.             else if (s == COMPLETING) // cannot time out yet  
  19.                 Thread.yield();  
  20.             else if (q == null)  
  21.                 q = new WaitNode();  
  22.             else if (!queued)  
  23.                 queued = UNSAFE.compareAndSwapObject(this, waitersOffset,  
  24.                                                      q.next = waiters, q);  
  25.             else if (timed) {  
  26.                 nanos = deadline - System.nanoTime();  
  27.                 if (nanos <= 0L) {  
  28.                     removeWaiter(q);  
  29.                     return state;  
  30.                 }  
  31.                 LockSupport.parkNanos(this, nanos);  
  32.             }  
  33.             else  
  34.                 LockSupport.park(this);  
  35.         }  
  36.     }  
       awaitDone的业务逻辑还是挺复杂的,我大致来分析下,第7行判断如果当前线程被中断的话,则抛出异常,第12行获取当前任务状态state,第13行判断如果任务状态大于COMPLETING的话,则直接返回state状态,当然从上面源码中的状态列表中可以发现大于COMPLETING的状态有5种,可能是有正常返回值的,也可能是抛出异常的,具体怎么处理等会在回答get方法的时候是会介绍的,第18行如果任务是正在执行的话,则让出一段CPU时间继续运行,接着第20和22行的判断其实就是判断等待结点和等待队列为空的话创建一个出来而已,接着如果我们在调用awaitDone的时候,设置的timed参数是true的话,则会执行26行处的if语句块,会设置线程等待我们设置的时间,等待时间到了会唤醒此线程,我们平常使用的get方法默认timed值是false的,因此会执行到第34行,将当前线程锁起来,你如果仔细点的话会发现第6到34行是个死循环,也就是当34行处的锁定在其他地方(其实就是set方法里面了)解开的话,仍然会继续执行第6行,那么因为此时state状态已经发生了改变,此时执行已经和之前执行流程不同啦,一般来讲的话,会执行到第13行进行判断,最后执行第16行返回状态码就可以了;

        那么get调用了LockSupport.park将自己锁住了,那么由谁来解锁呢?答案就是在set方法里面了,在刚刚的set方法的最后会执行finishCompletion方法,这个方法其实就是来解锁的啦:

[java] view plain copy
  1. private void finishCompletion() {  
  2.        // assert state > COMPLETING;  
  3.        for (WaitNode q; (q = waiters) != null;) {  
  4.            if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {  
  5.                for (;;) {  
  6.                    Thread t = q.thread;  
  7.                    if (t != null) {  
  8.                        q.thread = null;  
  9.                        LockSupport.unpark(t);  
  10.                    }  
  11.                    WaitNode next = q.next;  
  12.                    if (next == null)  
  13.                        break;  
  14.                    q.next = null// unlink to help gc  
  15.                    q = next;  
  16.                }  
  17.                break;  
  18.            }  
  19.        }  
  20.   
  21.        done();  
  22.   
  23.        callable = null;        // to reduce footprint  
  24.    }  
        可以看到第9行执行了LockSupport.unpark,相当于解锁了get方法中的LockSupport.park操作,这样的话awaitDone方法就会返回了,回答get方法里面:

[java] view plain copy
  1. public V get() throws InterruptedException, ExecutionException {  
  2.         int s = state;  
  3.         if (s <= COMPLETING)  
  4.             s = awaitDone(false, 0L);  
  5.         return report(s);  
  6.     }  
        在通过awaitDone获得返回的state状态值之后,就会调用report方法,查看report方法:

[java] view plain copy
  1. private V report(int s) throws ExecutionException {  
  2.     Object x = outcome;  
  3.     if (s == NORMAL)  
  4.         return (V)x;  
  5.     if (s >= CANCELLED)  
  6.         throw new CancellationException();  
  7.     throw new ExecutionException((Throwable)x);  
  8. }  
        其实很简单了,就是根据state状态来进行相应的操作了,如果state只等于NORMAL的话,会直接返回值了,也就是我们的get方法返回值了,这就是我们自己通过FutureTask获得Callable返回值的源码过程以及为什么get方法会阻塞;

        那么最后一个问题就是使用线程池方式获得Callable返回值的话,JDK为我们封装了什么?

        首选当然应该从submit方法看起了,这个方法是在AbstractExecutorService类里面的:

[java] view plain copy
  1. public Future<?> submit(Runnable task) {  
  2.       if (task == nullthrow new NullPointerException();  
  3.       RunnableFuture<Void> ftask = newTaskFor(task, null);  
  4.       execute(ftask);  
  5.       return ftask;  
  6.   }  
        可以看到使用线程池的方式实际上内部是通过为我们创建一个RunnableFuture对象并且返回这个对象的,这里的RunnableFuture对象实际上是FutureTask类型的,因为我们查看newTaskFor的实现可以发现:

[java] view plain copy
  1. protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {  
  2.      return new FutureTask<T>(callable);  
  3.  }  
        实际上就是将Callable对象封装成了FutureTask对象为我们返回而已了;

        到此,使用线程池来获得Callable任务返回值的神秘面纱被揭开了,即使我们自己没有显式创建FutureTask对象,但是JDK为我们创建了一个出来,之后的执行过程就和我们自己创建线程实现一致啦;

        至此,我对于Callable和Runnable的总结结束啦,谢谢大家访问!

0 0
原创粉丝点击