Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
来源:互联网 发布:企业版域名注册通 编辑:程序博客网 时间:2024/05/15 04:42
继续并发专题~
FutureTask 有点类似Runnable,都可以通过Thread来启动,不过FutureTask可以返回执行完毕的数据,并且FutureTask的get方法支持阻塞。
由于:FutureTask可以返回执行完毕的数据,并且FutureTask的get方法支持阻塞这两个特性,我们可以用来预先加载一些可能用到资源,然后要用的时候,调用get方法获取(如果资源加载完,直接返回;否则继续等待其加载完成)。
下面通过两个例子来介绍下:
1、使用FutureTask来预加载稍后要用的的数据。
- package com.zhy.concurrency.futuretask;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.FutureTask;
- /**
- * 使用FutureTask来提前加载稍后要用到的数据
- *
- * @author zhy
- *
- */
- public class PreLoaderUseFutureTask
- {
- /**
- * 创建一个FutureTask用来加载资源
- */
- private final FutureTask<String> futureTask = new FutureTask<String>(
- new Callable<String>()
- {
- @Override
- public String call() throws Exception
- {
- Thread.sleep(3000);
- return “加载资源需要3秒”;
- }
- });
- public final Thread thread = new Thread(futureTask);
- public void start()
- {
- thread.start();
- }
- /**
- * 获取资源
- *
- * @return
- * @throws ExecutionException
- * @throws InterruptedException
- */
- public String getRes() throws InterruptedException, ExecutionException
- {
- return futureTask.get();//加载完毕直接返回,否则等待加载完毕
- }
- public static void main(String[] args) throws InterruptedException, ExecutionException
- {
- PreLoaderUseFutureTask task = new PreLoaderUseFutureTask();
- /**
- * 开启预加载资源
- */
- task.start();
- // 用户在真正需要加载资源前进行了其他操作了2秒
- Thread.sleep(2000);
- /**
- * 获取资源
- */
- System.out.println(System.currentTimeMillis() + ”:开始加载资源”);
- String res = task.getRes();
- System.out.println(res);
- System.out.println(System.currentTimeMillis() + ”:加载资源结束”);
- }
- }
package com.zhy.concurrency.futuretask;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.FutureTask;/** * 使用FutureTask来提前加载稍后要用到的数据 * * @author zhy * */public class PreLoaderUseFutureTask{ /** * 创建一个FutureTask用来加载资源 */ private final FutureTask<String> futureTask = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(3000); return "加载资源需要3秒"; } }); public final Thread thread = new Thread(futureTask); public void start() { thread.start(); } /** * 获取资源 * * @return * @throws ExecutionException * @throws InterruptedException */ public String getRes() throws InterruptedException, ExecutionException { return futureTask.get();//加载完毕直接返回,否则等待加载完毕 } public static void main(String[] args) throws InterruptedException, ExecutionException { PreLoaderUseFutureTask task = new PreLoaderUseFutureTask(); /** * 开启预加载资源 */ task.start(); // 用户在真正需要加载资源前进行了其他操作了2秒 Thread.sleep(2000); /** * 获取资源 */ System.out.println(System.currentTimeMillis() + ":开始加载资源"); String res = task.getRes(); System.out.println(res); System.out.println(System.currentTimeMillis() + ":加载资源结束"); }}
运行结果:
- 1400902789275:开始加载资源
- 加载资源需要3秒
- 1400902790275:加载资源结束
1400902789275:开始加载资源加载资源需要3秒1400902790275:加载资源结束可以看到,本来加载资源的时间需要3秒,现在只花费了1秒,如果用户其他操作时间更长,则可直接返回,极大增加了用户体验。
2、看下Future的API
可以看到Future的API,还是比简单的,见名知意的感觉,get( long , TimeUnit )还能支持,设置最大等待时间,比如某个操作耗时太长,就可以取消了。
3、FutureTask模拟,用户在线观看电子书的预加载功能
用户观看当前页时,后台预先把下一页加载好,这样可以大幅度提高用户的体验,不需要每一页都等待加载,用户会觉得此电子书软件很流畅,哈哈,用户觉得好,才是真的好。
- package com.zhy.concurrency.futuretask;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.FutureTask;
- /**
- * 使用FutureTask模拟预加载下一页图书的内容
- *
- * @author zhy
- *
- */
- public class BookInstance
- {
- /**
- * 当前的页码
- */
- private volatile int currentPage = 1;
- /**
- * 异步的任务获取当前页的内容
- */
- FutureTask<String> futureTask = new FutureTask<String>(
- new Callable<String>()
- {
- @Override
- public String call() throws Exception
- {
- return loadDataFromNet();
- }
- });
- /**
- * 实例化一本书,并传入当前读到的页码
- *
- * @param currentPage
- */
- public BookInstance(int currentPage)
- {
- this.currentPage = currentPage;
- /**
- * 直接启动线程获取当前页码内容
- */
- Thread thread = new Thread(futureTask);
- thread.start();
- }
- /**
- * 获取当前页的内容
- *
- * @return
- * @throws InterruptedException
- * @throws ExecutionException
- */
- public String getCurrentPageContent() throws InterruptedException,
- ExecutionException
- {
- String con = futureTask.get();
- this.currentPage = currentPage + 1;
- Thread thread = new Thread(futureTask = new FutureTask<String>(
- new Callable<String>()
- {
- @Override
- public String call() throws Exception
- {
- return loadDataFromNet();
- }
- }));
- thread.start();
- return con;
- }
- /**
- * 根据页码从网络抓取数据
- *
- * @return
- * @throws InterruptedException
- */
- private String loadDataFromNet() throws InterruptedException
- {
- Thread.sleep(1000);
- return “Page ” + this.currentPage + “ : the content ….”;
- }
- public static void main(String[] args) throws InterruptedException,
- ExecutionException
- {
- BookInstance instance = new BookInstance(1);
- for (int i = 0; i < 10; i++)
- {
- long start = System.currentTimeMillis();
- String content = instance.getCurrentPageContent();
- System.out.println(”[1秒阅读时间]read:” + content);
- Thread.sleep(1000);
- System.out.println(System.currentTimeMillis() - start);
- }
- }
- }
package com.zhy.concurrency.futuretask;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.FutureTask;/** * 使用FutureTask模拟预加载下一页图书的内容 * * @author zhy * */public class BookInstance{ /** * 当前的页码 */ private volatile int currentPage = 1; /** * 异步的任务获取当前页的内容 */ FutureTask<String> futureTask = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { return loadDataFromNet(); } }); /** * 实例化一本书,并传入当前读到的页码 * * @param currentPage */ public BookInstance(int currentPage) { this.currentPage = currentPage; /** * 直接启动线程获取当前页码内容 */ Thread thread = new Thread(futureTask); thread.start(); } /** * 获取当前页的内容 * * @return * @throws InterruptedException * @throws ExecutionException */ public String getCurrentPageContent() throws InterruptedException, ExecutionException { String con = futureTask.get(); this.currentPage = currentPage + 1; Thread thread = new Thread(futureTask = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { return loadDataFromNet(); } })); thread.start(); return con; } /** * 根据页码从网络抓取数据 * * @return * @throws InterruptedException */ private String loadDataFromNet() throws InterruptedException { Thread.sleep(1000); return "Page " + this.currentPage + " : the content ...."; } public static void main(String[] args) throws InterruptedException, ExecutionException { BookInstance instance = new BookInstance(1); for (int i = 0; i < 10; i++) { long start = System.currentTimeMillis(); String content = instance.getCurrentPageContent(); System.out.println("[1秒阅读时间]read:" + content); Thread.sleep(1000); System.out.println(System.currentTimeMillis() - start); } }}
输出结果:
- [1秒阅读时间]read:Page 1 : the content ….
- 2001
- [1秒阅读时间]read:Page 2 : the content ….
- 1000
- [1秒阅读时间]read:Page 3 : the content ….
- 1001
- [1秒阅读时间]read:Page 4 : the content ….
- 1000
- [1秒阅读时间]read:Page 5 : the content ….
- 1001
[1秒阅读时间]read:Page 1 : the content ....2001[1秒阅读时间]read:Page 2 : the content ....1000[1秒阅读时间]read:Page 3 : the content ....1001[1秒阅读时间]read:Page 4 : the content ....1000[1秒阅读时间]read:Page 5 : the content ....1001
可以看到,除了第一次观看当前页需要等待网络加载数据的过程(输出的:2001,1000是加载耗时,1000是用户阅读时间),接下来的页面都是瞬间返回(输出的1000是用户阅读时间),完全不需要等待。
代码都是为了讲解FutureTask的应用场景,,,请勿直接在项目中使用。
好了,就到这里,欢迎各位留言。
转载自:http://blog.csdn.net/lmj623565791/article/details/26817403
阅读全文
0 0
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- Java 并发专题 :FutureTask 实现预加载数据
- 实现在线看电子书
- js缓存客户端浏览器用户网页浏览时间等信息数据实例
- 网页预加载---提升网页浏览速度
- Java多线程/并发20、Future实现类:FutureTask
- Java并发编程:Callable、Future和FutureTask的实现
- Java并发实战:Callable+FutureTask实现多线程判分汇总
- Java模拟实现百度文档在线浏览
- Java模拟实现百度文档在线浏览
- FlexPaper+SWFTools ,java实现在线文档浏览
- 八大排序算法图文动态 讲解
- MVC4使用VueJS入门
- MTU
- 索引与优化
- 理解gluPerspective和gluLookAt的关系
- Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等
- openstack attach volume过程详解
- 机器学习笔记——逻辑回归模型及其代价函数推导
- POJ 2387 Til the Cows Come Home
- ES6学习笔记:迭代器与生成器
- 每日MySQL之008:MySQL权限简介
- 基于Windows安装配置Apache、Tomcat、IIS服务器,Apache、Tomcat、IIS服务器对比
- 谜题1:奇偶性
- CCF认证201403第三题(提交未通过)