Future使用场景与分析
前面分享了CountDownLatch的用法,但是由于分享过程中,发现有些朋友,问我Future与CountDownLatch的有什么区别?
答案:只是concurrent包下的并发帮助工具类,两者并没有什么联系;对于CountDownLatch是关注与子线程的执行完毕情况,而Future是Callable执行call回调包装的返回值;
Runnable是执行工作的独立任务,但是它不返回任何值,如果希望任务完成时能够返回一个值,那么可以实现Callable接口,而不是实现Runnable接口,在Java 1.5中引入Callable是一种具有类型参数的泛型,它的类型参数,是从call()函数中获取到的,而不是run()方法,并且必须使用ExecutorService.submit()方法调用它;
package demo.test; import java.util.ArrayList;
import java.util.List;
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; /**
* futurn使用demo
*
* @author bqcoder
* @version $Id: FuturnDemo.java, v 0.1 2016年11月22日 下午9:07:13 bqcoder Exp $
*/
public class FutureDemo { public static void main(String[] args) { List<Future<String>> taskResults = new ArrayList<Future<String>>(); //创建线程池,使用future必须要使用executors.submit来调用,《乌龟的屁股,规定》
ExecutorService executor = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) {
Future<String> result = executor.submit(new TaskWithResult(i));
taskResults.add(result);
} //获取执行结果
for (int i = 0; i < 10; i++) {
try {
System.out.println(taskResults.get(i).get());
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
}
}
} class TaskWithResult implements Callable<String> { private int taskId; TaskWithResult(int taskId) {
this.taskId = taskId;
} @Override
public String call() throws Exception {
return "执行结果:任务taskId=" + taskId;
} }
运行结果:
执行结果:任务taskId=0
执行结果:任务taskId=1
执行结果:任务taskId=2
执行结果:任务taskId=3
执行结果:任务taskId=4
执行结果:任务taskId=5
执行结果:任务taskId=6
执行结果:任务taskId=7
执行结果:任务taskId=8
执行结果:任务taskId=9
submit()方法会产生Futurn对象,它用Callable返回结果的特定类型进行了参数化,可以使用Future.isDone()来判断Future查询是否完成,如果完成则返回ture,获取值通过get()方法进行获取,如果查询值还没有完成,则进入阻塞状态。
Future模式的核心在于:去除主线程的等待时间,将等待时间可以去处理其他复杂的业务逻辑。
Future模式有点类似于商品订单。在网上购物时,提交订单后,在收货的这段时间里无需一直在家里等候,可以先干别的事情。类推到程序设计中时,当提交请求时,期望得到答复时,如果这个答复可能很慢。传统的时一直等待到这个答复收到时再去做别的事情,但如果利用Future设计模式就无需等待答复的到来,在等待答复的过程中可以干其他事情。
附源码分析:
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;
}
}
}
}
参考资料:
1、【Thinking in Java】
2、【源码分析】http://blog.csdn.net/kobejayandy/article/details/46293927
3、【Java多线程编程中Future模式的详解】http://www.2cto.com/kf/201411/351903.html
Future使用场景与分析的更多相关文章
- 工业物联网或系统集成中应用消息队列(ActiveMQ,C#的demo)的场景全面分析
1.[连载]<C#通讯(串口和网络)框架的设计与实现> 2.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 2.应用SuperIO(SIO)和开源跨平台物联网框 ...
- 【Cocos游戏实战】功夫小子第七课之游戏主功能场景逻辑功能和暂停功能场景的分析和实现
CSDN的markdown编辑器是吃屎了么! !.什么玩意.!写了一半写不了东西还全没了,搞个毛线! 本节课的视频教程地址是:第七课在此 假设本教程有帮助到您,希望您能点击进去观看一下,并且如今注冊成 ...
- CountDownLatch使用场景及分析 并发测试
原文:https://www.cnblogs.com/bqcoder/p/6089101.html CountDownLatch使用场景及分析 JDk1.5提供了一个非常有用的包,Concurre ...
- JVM源码分析-类加载场景实例分析
A类调用B类的静态方法,除了加载B类,但是B类的一个未被调用的方法间接使用到的C类却也被加载了,这个有意思的场景来自一个提问:方法中使用的类型为何在未调用时尝试加载?. 场景如下: public cl ...
- 红黑树、B(+)树、跳表、AVL等数据结构,应用场景及分析,以及一些英文缩写
在网上学习了一些材料. 这一篇:https://www.zhihu.com/question/30527705 AVL树:最早的平衡二叉树之一.应用相对其他数据结构比较少.windows对进程地址空间 ...
- CountDownLatch使用场景及分析
JDk1.5提供了一个非常有用的包,Concurrent包,这个包主要用来操作一些并发操作,提供一些并发类,可以方便在项目当中傻瓜式应用. JDK1.5以前,使用并发操作,都是通过Thread,Run ...
- Activity启动场景Task分析(二)
场景分析 下面通过启动Activity的代码来分析一下: 1.桌面 首先,我们看下处于桌面时的状态,运行命令: adb shell dumpsys activity 结果如下 ACTIVITY MAN ...
- Chrome 插件特性及实战场景案例分析
一.前言 提起Chrome扩展插件(Chrome Extension),每个人的浏览器中或多或少都安装了几个插件,像一键翻译.广告屏蔽.录屏等等,通过使用这些插件,可以有效的提高我们的工作效率:但有时 ...
- smartjs - DataManager 场景示例分析 - 数据懒加载
发一张policy的参数图设置图: 场景1 - 数据的懒加载/延迟加载 在很多时候,为了提高网页的加载速度,减少不必要的开销,会将页面的数据拆分成几个部分,首先加载呈现可视区域内的数据,然后剩下来的会 ...
随机推荐
- 18、bootStap JavaScript插件
1.模态框 <!--模态框经过了优化,更加灵活,以弹出对话框的形式出现,具有最小和最实用的功能集.--> <button type="button" class= ...
- js 全局变量和局部变量
Javascript在执行前会对整个脚本文件的声明部分做完整分析(包括局部变量),但是不能对变量定义做提前解析 1.作用域和作用域链 2.变量声明提前
- pdb在python程序中应用
1.什么是pdb? pdb是python提供的调试程序的一种工具. 2.为什么需要pdb模块? 当我们的程序越写越大的时候,我们用print xxx 这种方式打断点,调试,非常不方便,这个时候我们需要 ...
- NodeJs01 文件浏览器
ES6常用新语法 前言 是时候学点新的JS了! 为了在学习NodeJs之前,能及时用上语言的新特性,我们打算从一开始先学习一下JavaScript语言的最基本最常用新语法.本课程的内容,是已经假设你有 ...
- nyoj 题目36 最长公共子序列
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- free、vmstat监视内存使用情况
9. free 查询可用内存 free工具用来查看系统可用内存: /opt/app/tdev1$free total used free shared buffers cached Mem: 8175 ...
- js中prop和attr区别
首先 attr 是从页面搜索获得元素值,所以页面必须明确定义元素才能获取值,相对来说比较慢. 如: <input name='test' type='checkbox'> $('input ...
- axis2实践(一)JAX-WS入门示例
1. 实例说明 现在大多数的网站都有通知功能(例如,放假通知,网站维护通知等),本实例就是针对于通知,发布两个WebService服务 1)根据供应商编号,状态,发布日期查询通知信息 2)根据编号查询 ...
- 【POJ3415】 Common Substrings (SA+单调栈)
这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了... 解法参考论文以及下面的链接 http://www.cnblogs.com/vongang/archive/2012/11/20 ...
- POJ 3907 Build Your Home | 计算多边形面积
给个多边形 计算面积 输出要四舍五入 直接用向量叉乘就好 四舍五入可以+0.5向下取整 #include<cstdio> #include<algorithm> #includ ...