多线程进阶--Future与Callable

Future与Callable可以实现一种异步计算结果,Callable类似于一个增强的Runnable接口,和Runnable接口不同的是,它提供了一个call方法来执行线程代码,而call方法可以有返回值,也可以声明式的抛出异常。
Future的get方法会阻塞在那,等待执行完成,获取结果。
在整个FeatureTask中没有直接使用锁机制,而是通过LockSupport来阻塞线程,唤醒线程。对于多线程访问FeatureTask的waiters,state,都是采用Unsafe(前面有介绍)来操作,避免使用锁(毕竟锁其实很耗时),改为直接原子操作对应的变量。FeatureTask是一个非常好的Unsafe和LockSupport例子

相关类简介

LockSupport
LockSupport是用来创建锁及其他同步类的基本线程阻塞元素,它的park和 unpark能够分别阻塞线程和解除线程阻塞。它提供了可以指定阻塞时长的park方法。park和unpark的基本接口为:

public static void park() {
    unsafe.park(false, 0L);
}
public static void unpark(Thread thread) {
    if (thread != null)
        unsafe.unpark(thread);
}

unpark需要指定对应的线程,而park是将当前线程阻塞。其实这里也提供阻塞线程新的方式,我们已经或者用忙等待,或者用中断睡眠的方式阻塞唤醒线程,这里可以通过LockSupport实现。FutureTask就是用这种方式实现的。下面看一下unsafe,它是一个Unsafe类

Unsafe

Java不能够直接访问操作系统底层,而是通过本地方法来访问。Unsafe提供了硬件级别的原子访问,主要提供一下功能:
1. 分配释放内存
2. 定位某个字段的内存位置
3. 挂起一个线程和恢复,更多的是通过LockSupport来访问。park和unpark
4. CAS操作,比较一个对象的某个位置的内存值是否与期望值一致,一致则更新对应值,此更新是不可中断的。主要方法是compareAndSwap*。

先举个栗子:

import java.util.concurrent.Callable;

class MyThread implements Callable<Integer>{

    /**
     * 实现call方法,作为线程代码
     * 这里线程可以有返回值
     */
    public Integer call() throws Exception {
        System.out.println("call开始执行"+System.currentTimeMillis());
        Thread.sleep(3000);
        System.out.println("call执行完成"+System.currentTimeMillis());
        return 1;
    }

}

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableFuture {

    public static void main(String[] args) {
        //创建callable对象
        MyThread callable = new MyThread();
        //使用FutureTask装callable
        FutureTask<Integer> task = new FutureTask<Integer>(callable);
        //启动线程,其实还是启动的callable,这是callable作为载体传入了线程对象
        new Thread(task,"有返回值的线程").start();
//          Thread.sleep(4000);
        System.out.println("main"); 
        try {
            System.out.println("线程的返回值:"+task.get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

    }
}

输出结果:
main
call开始执行1509447904091
call执行完成1509447907091
线程的返回值:1

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableFuture {

    public static void main(String[] args) {
        //创建callable对象
        MyThread callable = new MyThread();
        //使用FutureTask装callable
        FutureTask<Integer> task = new FutureTask<Integer>(callable);
        //启动线程,其实还是启动的callable,这是callable作为载体传入了线程对象
        new Thread(task,"有返回值的线程").start();
//          Thread.sleep(4000);

        try {
            System.out.println("线程的返回值:"+task.get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        System.out.println("main"); 
    }
}

输出结果
call开始执行1509447971746
call执行完成1509447974746
线程的返回值:1
main

结果对比,可以看出来当主线程执行到task.get()的时候,若任务线程没有执行完毕,主线程会阻塞等待任务线程的执行直到有返回值

先来看看callable和future是怎么实现为我们返回异步运行结果并且在返回结果之前阻塞主线程的
贴源码前先来看下FutureTask和Callable的继承关系:
这里写图片描述
贴上FutureTask源码:

public class FutureTask<V> implements RunnableFuture<V> {  
    /** 所有的方法全部委托sync */  
    private final Sync sync;  

    public FutureTask(Callable<V> callable) {  
        if (callable == null)  
            throw new NullPointerException();  
        sync = new Sync(callable);  
    }  

    public FutureTask(Runnable runnable, V result) {  
        sync = new Sync(Executors.callable(runnable, result));  
    }  

    public boolean isCancelled() {  
        return sync.innerIsCancelled();  
    }  

    public boolean isDone() {  
        return sync.innerIsDone();  
    }  

    public boolean cancel(boolean mayInterruptIfRunning) {  
        return sync.innerCancel(mayInterruptIfRunning);  
    }  


    public V get() throws InterruptedException, ExecutionException {  
        return sync.innerGet();  
    }  

    public V get(long timeout, TimeUnit unit)  
        throws InterruptedException, ExecutionException, TimeoutException {  
        return sync.innerGet(unit.toNanos(timeout));  
    }  

    protected void done() { }  

    protected void set(V v) {  
        sync.innerSet(v);  
    }  

    protected void setException(Throwable t) {  
        sync.innerSetException(t);  
    }  

    public void run() {  
        sync.innerRun();  
    }  

    protected boolean runAndReset() {  
        return sync.innerRunAndReset();  
    }  

    private final class Sync extends AbstractQueuedSynchronizer {  
        private static final long serialVersionUID = -7828117401763700385L;  

        /** State value representing that task is ready to run */  
        /** 代表起始状态 */  
        private static final int READY     = 0;  
        /** State value representing that task is running */  
        /** 代表正在运行中状态 */  
        private static final int RUNNING   = 1;  
        /** State value representing that task ran */  
        /** 代表运行完成的状态 */  
        private static final int RAN       = 2;  
        /** State value representing that task was cancelled */  
        /** 代表被取消的状态 */  
        private static final int CANCELLED = 4;  

        /** The underlying callable */  
        private final Callable<V> callable;  
        /** The result to return from get() */  
        private V result;  
        /** The exception to throw from get() */  
        private Throwable exception;  

        /** 
         * The thread running task. When nulled after set/cancel, this 
         * indicates that the results are accessible.  Must be 
         * volatile, to ensure visibility upon completion. 
         */  
        private volatile Thread runner;  

        Sync(Callable<V> callable) {  
            this.callable = callable;  
        }  

        /** 
        *  判断是否完成或者是否取消 
        *  传入0或者1 都返回0 说明任务没有完成 也没有取消 
        */  
        private boolean ranOrCancelled(int state) {  
            return (state & (RAN | CANCELLED)) != 0;  
        }  

        /** 
         * AbstractQueuedSynchronizer的模板方法  
         * 返回1可以获取锁 返回-1说明获取锁失败 
         * 调用innerIsDone 返回TRUE 说明任务已经执行完毕 
         * 返回FALSE 说明任务没有执行完毕 
         */  
        protected int tryAcquireShared(int ignore) {  
            return innerIsDone() ? 1 : -1;  
        }  

        /** 
         * 释放锁 将执行当前任务的线程设置为null 
         */  
        protected boolean tryReleaseShared(int ignore) {  
            runner = null;  
            return true;  
        }  

        //判断任务是否被取消  
        boolean innerIsCancelled() {  
            return getState() == CANCELLED;  
        }  

        //判断任务是否完成(取消也算完成)  
        boolean innerIsDone() {  
            return ranOrCancelled(getState()) && runner == null;  
        }  

        //获取结果  
        V innerGet() throws InterruptedException, ExecutionException {  
            //首先调用AbstractQueuedSynchronizer的方法,这个方法会调用子类方法tryAcquireShared 上面有讲  
            //如果当前任务已经完成,那么当前线程可以向下运行,否则把当前线程加入队列阻塞.就是在这里实
            //现的主线程等待任务线程执行结果的部分,这部分源码深入分析的话后面在看  
            acquireSharedInterruptibly(0);  
            //判断状态 如果取消了就抛CancellationException异常.  
            if (getState() == CANCELLED)  
                throw new CancellationException();  
            //如果任务执行过程中出现异常,这里包装一下抛出ExecutionException.  
            if (exception != null)  
                throw new ExecutionException(exception);  
            return result;  
        }  

        //获取结果  
        V innerGet(long nanosTimeout) throws InterruptedException, ExecutionException, TimeoutException {  
            //调用AbstractQueuedSynchronizer里的方法  
            // return tryAcquireShared(arg) >= 0 ||doAcquireSharedNanos(arg, nanosTimeout);  
            // 首先tryAcquireShared调用它获取锁,也就是看任务完事没,如果任务完事了就返回TRUE,那么执行逻辑同上。  
            // 如果获取不到锁,那么就阻塞当前线程给定的时间,如果时间到了再次任务还没完成则抛出异常。

            if (!tryAcquireSharedNanos(0, nanosTimeout))  
                throw new TimeoutException();  
            if (getState() == CANCELLED)  
                throw new CancellationException();  
            if (exception != null)  
                throw new ExecutionException(exception);  
            return result;  
        }  


        void innerSet(V v) {  
            for (;;) {  
                int s = getState();  
                if (s == RAN)  
                    return;  
                if (s == CANCELLED) {  
                    // aggressively release to set runner to null,  
                    // in case we are racing with a cancel request  
                    // that will try to interrupt runner  
                    releaseShared(0);  
                    return;  
                }  
                //正常完成 设置状态为RAN  
                if (compareAndSetState(s, RAN)) {  
                    result = v;  
                    releaseShared(0);  
                    done(); //通知子类  
                    return;  
                }  
            }  
        }  

        void innerSetException(Throwable t) {  
            for (;;) {  
                int s = getState();  
                if (s == RAN)  
                    return;  
                if (s == CANCELLED) {  
                    // aggressively release to set runner to null,  
                    // in case we are racing with a cancel request  
                    // that will try to interrupt runner  
                    releaseShared(0);  
                    return;  
                }  
                //设置异常  
                if (compareAndSetState(s, RAN)) {  
                    exception = t;  
                    releaseShared(0);  
                    done();//通知子类  
                    return;  
                }  
            }  
        }  

        //取消任务  
        boolean innerCancel(boolean mayInterruptIfRunning) {  
            for (;;) {  
                int s = getState();  
                //如果任务已经结束,则返回FALSE  
                if (ranOrCancelled(s))  
                    return false;  
                //设置任务的状态为CANCELLED  
                if (compareAndSetState(s, CANCELLED))  
                    break;  
            }  
            //如果参数mayInterruptIfRunning=TRUE,那么设置线程的终端状态  
            if (mayInterruptIfRunning) {  
                Thread r = runner;  
                if (r != null)  
                    r.interrupt();  
            }  
            //释放锁  
            releaseShared(0);  
            //调用子类方法,通知状态改变  
            done();  
            return true;  
        }  

        void innerRun() {  
            //如果任务不是初始状态则直接结束  
            if (!compareAndSetState(READY, RUNNING))  
                return;  

            runner = Thread.currentThread();  
            if (getState() == RUNNING) { // recheck after setting thread  
                V result;  
                try {  
                    result = callable.call();  
                } catch (Throwable ex) {  
                    //我们写的任务方法里如果出现异常则调用setException  
                    setException(ex);  
                    return;  
                }  
                //设置结果  
                set(result);  
            } else {  
                //释放锁  
                releaseShared(0); // cancel  
            }  
        }  

        boolean innerRunAndReset() {  
            if (!compareAndSetState(READY, RUNNING))  
                return false;  
            try {  
                runner = Thread.currentThread();  
                if (getState() == RUNNING)  
                    callable.call(); // don't set result  
                runner = null;  
                return compareAndSetState(RUNNING, READY);  
            } catch (Throwable ex) {  
                setException(ex);  
                return false;  
            }  
        }  
    }  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值