在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java 并发专题 :FutureTask 实现预加载数据 在线看电子书、浏览器浏览网页等

2017-2-7 13:39| 发布者: zhangjf| 查看: 455| 评论: 0

摘要: 继续并发专题~ FutureTask 有点类似Runnable,都可以通过Thread来启动,不过FutureTask可以返回执行完毕的数据,并且FutureTask的get方法支持阻塞。 由于:FutureTask可以返回执行完毕的数据,并且FutureTask的get ...

继续并发专题~

FutureTask 有点类似Runnable,都可以通过Thread来启动,不过FutureTask可以返回执行完毕的数据,并且FutureTask的get方法支持阻塞。

由于:FutureTask可以返回执行完毕的数据,并且FutureTask的get方法支持阻塞这两个特性,我们可以用来预先加载一些可能用到资源,然后要用的时候,调用get方法获取(如果资源加载完,直接返回;否则继续等待其加载完成)。

下面通过两个例子来介绍下:

1、使用FutureTask来预加载稍后要用的的数据。

  1. package com.zhy.concurrency.futuretask;
  2. import java.util.concurrent.Callable;
  3. import java.util.concurrent.ExecutionException;
  4. import java.util.concurrent.FutureTask;
  5. /**
  6. * 使用FutureTask来提前加载稍后要用到的数据
  7. *
  8. * @author zhy
  9. *
  10. */
  11. public class PreLoaderUseFutureTask
  12. {
  13. /**
  14. * 创建一个FutureTask用来加载资源
  15. */
  16. private final FutureTask<String> futureTask = new FutureTask<String>(
  17. new Callable<String>()
  18. {
  19. @Override
  20. public String call() throws Exception
  21. {
  22. Thread.sleep(3000);
  23. return "加载资源需要3秒";
  24. }
  25. });
  26. public final Thread thread = new Thread(futureTask);
  27. public void start()
  28. {
  29. thread.start();
  30. }
  31. /**
  32. * 获取资源
  33. *
  34. * @return
  35. * @throws ExecutionException
  36. * @throws InterruptedException
  37. */
  38. public String getRes() throws InterruptedException, ExecutionException
  39. {
  40. return futureTask.get();//加载完毕直接返回,否则等待加载完毕
  41. }
  42. public static void main(String[] args) throws InterruptedException, ExecutionException
  43. {
  44. PreLoaderUseFutureTask task = new PreLoaderUseFutureTask();
  45. /**
  46. * 开启预加载资源
  47. */
  48. task.start();
  49. // 用户在真正需要加载资源前进行了其他操作了2秒
  50. Thread.sleep(2000);
  51. /**
  52. * 获取资源
  53. */
  54. System.out.println(System.currentTimeMillis() + ":开始加载资源");
  55. String res = task.getRes();
  56. System.out.println(res);
  57. System.out.println(System.currentTimeMillis() + ":加载资源结束");
  58. }
  59. }
复制代码

运行结果:

  1. 1400902789275:开始加载资源
  2. 加载资源需要3秒
  3. 1400902790275:加载资源结束
复制代码
可以看到,本来加载资源的时间需要3秒,现在只花费了1秒,如果用户其他操作时间更长,则可直接返回,极大增加了用户体验。

2、看下Future的API


可以看到Future的API,还是比简单的,见名知意的感觉,get( long , TimeUnit )还能支持,设置最大等待时间,比如某个操作耗时太长,就可以取消了。

3、FutureTask模拟,用户在线观看电子书的预加载功能

用户观看当前页时,后台预先把下一页加载好,这样可以大幅度提高用户的体验,不需要每一页都等待加载,用户会觉得此电子书软件很流畅,哈哈,用户觉得好,才是真的好。

  1. package com.zhy.concurrency.futuretask;
  2. import java.util.concurrent.Callable;
  3. import java.util.concurrent.ExecutionException;
  4. import java.util.concurrent.FutureTask;
  5. /**
  6. * 使用FutureTask模拟预加载下一页图书的内容
  7. *
  8. * @author zhy
  9. *
  10. */
  11. public class BookInstance
  12. {
  13. /**
  14. * 当前的页码
  15. */
  16. private volatile int currentPage = 1;
  17. /**
  18. * 异步的任务获取当前页的内容
  19. */
  20. FutureTask<String> futureTask = new FutureTask<String>(
  21. new Callable<String>()
  22. {
  23. @Override
  24. public String call() throws Exception
  25. {
  26. return loadDataFromNet();
  27. }
  28. });
  29. /**
  30. * 实例化一本书,并传入当前读到的页码
  31. *
  32. * @param currentPage
  33. */
  34. public BookInstance(int currentPage)
  35. {
  36. this.currentPage = currentPage;
  37. /**
  38. * 直接启动线程获取当前页码内容
  39. */
  40. Thread thread = new Thread(futureTask);
  41. thread.start();
  42. }
  43. /**
  44. * 获取当前页的内容
  45. *
  46. * @return
  47. * @throws InterruptedException
  48. * @throws ExecutionException
  49. */
  50. public String getCurrentPageContent() throws InterruptedException,
  51. ExecutionException
  52. {
  53. String con = futureTask.get();
  54. this.currentPage = currentPage + 1;
  55. Thread thread = new Thread(futureTask = new FutureTask<String>(
  56. new Callable<String>()
  57. {
  58. @Override
  59. public String call() throws Exception
  60. {
  61. return loadDataFromNet();
  62. }
  63. }));
  64. thread.start();
  65. return con;
  66. }
  67. /**
  68. * 根据页码从网络抓取数据
  69. *
  70. * @return
  71. * @throws InterruptedException
  72. */
  73. private String loadDataFromNet() throws InterruptedException
  74. {
  75. Thread.sleep(1000);
  76. return "Page " + this.currentPage + " : the content ....";
  77. }
  78. public static void main(String[] args) throws InterruptedException,
  79. ExecutionException
  80. {
  81. BookInstance instance = new BookInstance(1);
  82. for (int i = 0; i < 10; i++)
  83. {
  84. long start = System.currentTimeMillis();
  85. String content = instance.getCurrentPageContent();
  86. System.out.println("[1秒阅读时间]read:" + content);
  87. Thread.sleep(1000);
  88. System.out.println(System.currentTimeMillis() - start);
  89. }
  90. }
  91. }
复制代码

输出结果:

  1. [1秒阅读时间]read:Page 1 : the content ....
  2. 2001
  3. [1秒阅读时间]read:Page 2 : the content ....
  4. 1000
  5. [1秒阅读时间]read:Page 3 : the content ....
  6. 1001
  7. [1秒阅读时间]read:Page 4 : the content ....
  8. 1000
  9. [1秒阅读时间]read:Page 5 : the content ....
  10. 1001
复制代码

可以看到,除了第一次观看当前页需要等待网络加载数据的过程(输出的:2001,1000是加载耗时,1000是用户阅读时间),接下来的页面都是瞬间返回(输出的1000是用户阅读时间),完全不需要等待。

代码都是为了讲解FutureTask的应用场景,,,请勿直接在项目中使用。


好了,就到这里,欢迎各位留言。



来自: http://blog.csdn.net//lmj623565791/article/details/26817403

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 06:52

Copyright 2015-2025 djqfx

返回顶部