朋的博客

MySQL资料,Java技术,管理思想,博弈论,Ajax,XP极限编程,H.264,HEVC,HDR
随笔 - 86, 文章 - 59, 评论 - 1069, 引用 - 0
数据加载中……

使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]


个人理解
Future 模式就是在主线程中当需要进行比较耗时的作业,但不想阻塞主线程的作业时,将耗时作业交由 Future 对象在后台中完成,当主线程将来(这个 Future 的意义也就体现在这里了)需要时即可通过 Future 对象获得已经作业对象。  

这里写了一个简单的例子来说明这种模式,其实写这个例子主要是自己想熟悉一下 JDK5 concurrency 包中 FutureTask 的用法了。例子模拟的是一个会计算账的过程,主线程中已经获得其他帐户的总额了,为了不让主线程等待 PrivateAccount 返回而启用新的线程去处理,并使用 FutureTask 对象来监控,最后需要计算总额的时候再尝试去获得 PrivateAccount 的信息。

 

代码如下:

 1 package  testCallable;
 2
 3 import  java.util.Random;
 4 import  java.util.concurrent.Callable;
 5 import  java.util.concurrent.ExecutionException;
 6 import  java.util.concurrent.FutureTask;
 7
 8 /**
 9  *  @author  chenpengyi
10   */

11 public   class  SumAccountExample  {
12
13      public   static   void  main(String[] args)  {
14          //  Init callable object and future task
15         Callable pAccount  =   new  PrivateAccount();
16         FutureTask futureTask  =   new  FutureTask(pAccount);
17         
18          //  Create a new thread to do so
19         Thread pAccountThread  =   new  Thread(futureTask);
20         pAccountThread.start();
21         
22          //  Do something else in the main thread
23         System.out.println( " Doing something else here. " );
24         
25          //  Get the total money from other accounts 
26          int  totalMoney  =   new  Random().nextInt( 100000 );
27         System.out.println( " You have  "   +  totalMoney  +   "  in your other Accounts. " );
28         System.out.println( " Waiting for data from Private Account " );
29          //  If the Future task is not finished, we will wait for it
30          while ( ! futureTask.isDone()) {
31              try   {
32                 Thread.sleep( 5 );
33             }
  catch  (InterruptedException e)  {
34                 e.printStackTrace();
35             }

36         }

37         Integer privataAccountMoney  =   null ;
38          //  Since the future task is done, get the object back
39          try   {
40             privataAccountMoney  =  (Integer)futureTask.get();
41         }
  catch  (InterruptedException e)  {
42             e.printStackTrace();
43         }
  catch  (ExecutionException e)  {
44             e.printStackTrace();
45         }

46         System.out.println( " The total moeny you have is  "   +  (totalMoney  +  privataAccountMoney.intValue()));
47     }

48
49 }

50
51
52 class  PrivateAccount  implements  Callable {
53
54     Integer totalMoney;
55     
56     @Override
57      public  Integer call()  throws  Exception  {
58          //  Simulates a time conusimg task, sleep for 10s
59         Thread.sleep( 10000 );
60         totalMoney  =   new  Integer( new  Random().nextInt( 10000 ));
61         System.out.println( " You have  "   +  totalMoney  +   "  in your private Account. " );
62          return  totalMoney;
63     }

64     
65 }

主线程获得了返回后即完成了总额的计算。

posted on 2007-11-01 13:11 benchensz 阅读(4369) 评论(8)  编辑  收藏 所属分类: 随便写写(比较有用,值得看看)

评论

# re: 使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]  回复  更多评论   

Good!
2007-11-01 20:56 | 猪头小队长

# re: 使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]  回复  更多评论   

陈先生,陈太也来这里捣乱了,哼哼
捣乱捣乱,不安不安
欺负你
欺负你
哼哼!!!
2008-03-09 23:20 | 陈太

# re: 使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]  回复  更多评论   

陈先生,这里的东西看不懂,
因为没有陈太很漂漂的照片也,
发表一下爱的宣言,写个小程序嘛
还要用VFP哦
呵呵
看你这里挺多人捧场的,o(∩_∩)o...,都不知道那个计量经济学术语有什么用
那么多人赞你
威威地啦。。。陈先生!!!呵呵
2008-03-09 23:24 | 陈太

# re: 使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]  回复  更多评论   

如果能够在Future中加入Listener就好了,这样可以在Futher状态变化的时候callback回来,不然每次都去查询显示是不合理的
2008-06-12 10:09 | 伤桥

# re: 使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]  回复  更多评论   

不仅加上Listener,还需要在Futher中最好加上wait方法,某些必须要等到Futher执行完的动作可以在Listener中完成,也可以调用wait等待,这样就简单了
2008-06-12 10:12 | 伤桥

# cheap jordan  回复  更多评论   

My brother saved this web site for me and I have been going through it for the past several hrs. This is really going to assist me and my friends for our class project. By the way, I like the way you write.
2012-08-14 14:56 | cheap jordan

# re: 使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]  回复  更多评论   

很好的网站
2012-11-28 10:55 | wholesale cheap jerseys

# re: 使用Future模式完成耗时的背景作业[JDK1.5 Concurrency]  回复  更多评论   

它帮助我很多解决一些问题。其机会是如此美妙和工作组的麦粒肿,速度如此之快。我认为它可能会帮助你的所有。谢谢你。
2013-02-25 16:36 | write my essay

只有注册用户登录后才能发表评论。


网站导航: