java并发编程之Future异步响应模式

假设有如下一个场景:

假如你突然想做饭,但是没有厨具,也没有食材。网上购买厨具比较方便,食材去超市买更放心。

实现分析:在快递员送厨具的期间,我们肯定不会闲着,可以去超市买食材。所以,在主线程里面另起一个子线程去网购厨具。

但是,子线程执行的结果是要返回厨具的,而run方法是没有返回值的。所以,这才是难点

代码实现1:

package com.hystrix.future;
/**
 * Future异步模式应用
 * 场景:假如你突然想做饭,但是没有厨具,也没有食材。网上购买厨具比较方便,食材去超市买更放心。
     实现分析:在快递员送厨具的期间,我们肯定不会闲着,可以去超市买食材。所以,在主线程里面另起一个子线程去网购厨具。
                        但是,子线程执行的结果是要返回厨具的,而run方法是没有返回值的。所以,这才是难点,需要好好考虑一下
 * 
 *
 */
public class CommonCook {
    public static void main(String[] args) throws InterruptedException {
    	long startTime = System.currentTimeMillis();
		//第一步,网购厨具
    	OnlineShopping thread = new OnlineShopping();
    	thread.start();
    	//保证厨具送到
    	thread.join();
        //模拟食材购买时间
    	Thread.sleep(2000);
    	Shicai shicai = new Shicai();
    	System.out.println("第二步: 食材到位");
    	System.out.println("第三步: 开始展现厨艺");
    	cook(thread.chuju,shicai);
    	System.out.println("总共用时"+(System.currentTimeMillis()-startTime)+"ms");
    	
	}
    static class OnlineShopping extends Thread{
    	private Chuju chuju;

		@Override
		public void run() {
			System.out.println("第一步: 下单");
			System.out.println("第一步: 等待送货");
	        try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("第一步: 快递送到");
			chuju = new Chuju();
		}
    	
    }
    //厨具类
    static class Chuju{}
    //食材类
    static class Shicai{}
    //用厨具烹饪食材
    static void  cook(Chuju chuju,Shicai shicai) {}
    
}

运行结果如下:

第一步: 下单
第一步: 等待送货
第一步: 快递送到
第二步: 食材到位
第三步: 开始展现厨艺
总共用时7011ms

可以看到,多线程已经失去了意义。在厨具送到期间,我们不能干任何事。对应代码,就是调用join方法阻塞主线程。

有人问了,不阻塞主线程行不行???

不行!!!

从代码来看的话,run方法不执行完,属性chuju就没有被赋值,还是null。换句话说,没有厨具,怎么做饭。

Java现在的多线程机制,核心方法run是没有返回值的;如果要保存run方法里面的计算结果,必须等待run方法计算完,无论计算过程多么耗时。

面对这种尴尬的处境,程序员就会想:在子线程run方法计算的期间,能不能在主线程里面继续异步执行???

这种想法的核心就是Future模式,下面先应用一下Java自己实现的Future模式。

下面使用Future异步模式实现上述场景:

代码实现2:

package com.hystrix.future;

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

/**
 * 
 * 多线程Future模式
 *
 */
public class FutureCommonCook {
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		long startTime = System.currentTimeMillis();
		//第一步:网购厨具
		Callable<Chuju> onlineShoping = new Callable<FutureCommonCook.Chuju>() {

			@Override
			public Chuju call() throws Exception {
				System.out.println("第一步: 下单");
				System.out.println("第一步: 等待送货");
		        try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("第一步: 快递送到");
				return new Chuju();
			}
		};
		FutureTask<Chuju> task = new FutureTask<FutureCommonCook.Chuju>(onlineShoping);
		new Thread(task).start();
		//模拟食材购买时间
    	Thread.sleep(2000);
    	Shicai shicai = new Shicai();
    	System.out.println("第二步: 食材到位");
    	//第三步:用厨具烹饪食材
    	if(!task.isDone()) {//联系快递员询问是否到货
    		System.out.println("还没到货,心情好就等着(心情不好就调用cancel方法取消订单)");
    	}
    	Chuju chuju = task.get();
    	System.out.println("第三步:厨具到位,开始展现厨艺");
    	cook(chuju,shicai);
    	System.out.println("总共用时:"+(System.currentTimeMillis()-startTime+"ms"));
	}
	
		
	//厨具类
    static class Chuju{}
    //食材类
    static class Shicai{}
    //用厨具烹饪食材
    static void  cook(Chuju chuju,Shicai shicai) {}
}

运行结果如下:

第一步: 下单
第一步: 等待送货
第二步: 食材到位
还没到货,心情好就等着(心情不好就调用cancel方法取消订单)
第一步: 快递送到
第三步:厨具到位,开始展现厨艺
总共用时:5008ms

 可以看见,在快递员送厨具的期间,我们没有闲着,可以去买食材;而且我们知道厨具到没到,甚至可以在厨具没到的时候,取消订单不要了。

好神奇,有没有。

下面具体分析一下第二段代码:

1)把耗时的网购厨具逻辑,封装到了一个Callable的call方法里面。

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

 Callable接口可以看作是Runnable接口的补充,call方法带有返回值,并且可以抛出异常。

2)把Callable实例当作参数,生成一个FutureTask的对象,然后把这个对象当作一个Runnable,作为参数另起线程。


public class FutureTask<V> implements RunnableFuture<V>

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

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

这个继承体系中的核心接口是Future。Future的核心思想是:一个方法f,计算过程可能非常耗时,等待f返回,显然不明智。可以在调用f的时候,立马返回一个Future,可以通过Future这个数据结构去控制方法f的计算过程。

这里的控制包括:

get方法:获取计算结果(如果还没计算完,也是必须等待的)

cancel方法:还没计算完,可以取消计算过程

isDone方法:判断是否计算完

isCancelled方法:判断计算是否被取消

这些接口的设计很完美,FutureTask的实现注定不会简单,后面再说。

 

3)在第三步里面,调用了isDone方法查看状态,然后直接调用task.get方法获取厨具,不过这时还没送到,所以还是会等待3秒。对比第一段代码的执行结果,这里我们节省了2秒。这是因为在快递员送货期间,我们去超市购买食材,这两件事在同一时间段内异步执行。

通过以上3步,我们就完成了对Java原生Future模式最基本的应用。下面具体分析下FutureTask的实现

public void run() {
        if (state != NEW ||
            !UNSAFE.compareAndSwapObject(this, runnerOffset,
                                         null, Thread.currentThread()))
            return;
        try {
            Callable<V> c = callable; // 这里的callable是从构造方法里面传人的
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                    result = c.call();
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    setException(ex); // 保存call方法抛出的异常
                }
                if (ran)
                    set(result); // 保存call方法的执行结果
            }
        } 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);
        }
    }

先看try语句块里面的逻辑,发现run方法的主要逻辑就是运行Callable的call方法,然后将保存结果或者异常(用的一个属性result)。这里比较难想到的是,将call方法抛出的异常也保存起来了。

这里表示状态的属性state是个什么鬼

 * Possible state transitions:
     * NEW -> COMPLETING -> NORMAL
     * NEW -> COMPLETING -> EXCEPTIONAL
     * NEW -> CANCELLED
     * NEW -> INTERRUPTING -> INTERRUPTED
     */
    private volatile int state;
    private static final int NEW          = 0;
    private static final int COMPLETING   = 1;
    private static final int NORMAL       = 2;
    private static final int EXCEPTIONAL  = 3;
    private static final int CANCELLED    = 4;
    private static final int INTERRUPTING = 5;
    private static final int INTERRUPTED  = 6;

把FutureTask看作一个Future,那么它的作用就是控制Callable的call方法的执行过程,在执行的过程中自然会有状态的转换:

1)一个FutureTask新建出来,state就是NEW状态;COMPETING和INTERRUPTING用的进行时,表示瞬时状态,存在时间极短(为什么要设立这种状态???不解);NORMAL代表顺利完成;EXCEPTIONAL代表执行过程出现异常;CANCELED代表执行过程被取消;INTERRUPTED被中断

2)执行过程顺利完成:NEW -> COMPLETING -> NORMAL

3)执行过程出现异常:NEW -> COMPLETING -> EXCEPTIONAL

4)执行过程被取消:NEW -> CANCELLED

5)执行过程中,线程中断:NEW -> INTERRUPTING -> INTERRUPTED

代码中状态判断、CAS操作等细节,请读者自己阅读。

再看看get方法的实现:

  public V get() throws InterruptedException, ExecutionException {
        int s = state;
        if (s <= COMPLETING)
            s = awaitDone(false, 0L);
        return report(s);
    }
 private int awaitDone(boolean timed, long nanos)
        throws InterruptedException {
        final long deadline = timed ? System.nanoTime() + nanos : 0L;
        WaitNode q = null;
        boolean queued = false;
        for (;;) {
            if (Thread.interrupted()) {
                removeWaiter(q);
                throw new InterruptedException();
            }

            int s = state;
            if (s > COMPLETING) {
                if (q != null)
                    q.thread = null;
                return s;
            }
            else if (s == COMPLETING) // cannot time out yet
                Thread.yield();
            else if (q == null)
                q = new WaitNode();
            else if (!queued)
                queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
                                                     q.next = waiters, q);
            else if (timed) {
                nanos = deadline - System.nanoTime();
                if (nanos <= 0L) {
                    removeWaiter(q);
                    return state;
                }
                LockSupport.parkNanos(this, nanos);
            }
            else
                LockSupport.park(this);
        }
    }

get方法的逻辑很简单,如果call方法的执行过程已完成,就把结果给出去;如果未完成,就将当前线程挂起等待。awaitDone方法里面死循环的逻辑,推演几遍就能弄懂;它里面挂起线程的主要创新是定义了WaitNode类,来将多个等待线程组织成队列,这是与JDK6的实现最大的不同。

挂起的线程何时被唤醒:

 private void finishCompletion() {
        // assert state > COMPLETING;
        for (WaitNode q; (q = waiters) != null;) {
            if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {
                for (;;) {
                    Thread t = q.thread;
                    if (t != null) {
                        q.thread = null;
                        LockSupport.unpark(t); // 唤醒线程
                    }
                    WaitNode next = q.next;
                    if (next == null)
                        break;
                    q.next = null; // unlink to help gc
                    q = next;
                }
                break;
            }
        }

        done();

        callable = null;        // to reduce footprint
    }

以上便是java中Future异步模式的大致实现过程

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值