<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    朋的博客

    MySQL資料,Java技術(shù),管理思想,博弈論,Ajax,XP極限編程,H.264,HEVC,HDR
    隨筆 - 86, 文章 - 59, 評論 - 1069, 引用 - 0
    數(shù)據(jù)加載中……

    使用Future模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]


    個(gè)人理解
    Future 模式就是在主線程中當(dāng)需要進(jìn)行比較耗時(shí)的作業(yè),但不想阻塞主線程的作業(yè)時(shí),將耗時(shí)作業(yè)交由 Future 對象在后臺中完成,當(dāng)主線程將來(這個(gè) Future 的意義也就體現(xiàn)在這里了)需要時(shí)即可通過 Future 對象獲得已經(jīng)作業(yè)對象。 ?

    這里寫了一個(gè)簡單的例子來說明這種模式,其實(shí)寫這個(gè)例子主要是自己想熟悉一下 JDK5 concurrency 包中 FutureTask 的用法了。例子模擬的是一個(gè)會計(jì)算賬的過程,主線程中已經(jīng)獲得其他帳戶的總額了,為了不讓主線程等待 PrivateAccount 返回而啟用新的線程去處理,并使用 FutureTask 對象來監(jiān)控,最后需要計(jì)算總額的時(shí)候再嘗試去獲得 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 }

    主線程獲得了返回后即完成了總額的計(jì)算。

    posted on 2007-11-01 13:11 benchensz 閱讀(4416) 評論(8)  編輯  收藏 所屬分類: 隨便寫寫(比較有用,值得看看)

    評論

    # re: 使用Future模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]  回復(fù)  更多評論   

    Good!
    2007-11-01 20:56 | 豬頭小隊(duì)長

    # re: 使用Future模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]  回復(fù)  更多評論   

    陳先生,陳太也來這里搗亂了,哼哼
    搗亂搗亂,不安不安
    欺負(fù)你
    欺負(fù)你
    哼哼!!!
    2008-03-09 23:20 | 陳太

    # re: 使用Future模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]  回復(fù)  更多評論   

    陳先生,這里的東西看不懂,
    因?yàn)闆]有陳太很漂漂的照片也,
    發(fā)表一下愛的宣言,寫個(gè)小程序嘛
    還要用VFP哦
    呵呵
    看你這里挺多人捧場的,o(∩_∩)o...,都不知道那個(gè)計(jì)量經(jīng)濟(jì)學(xué)術(shù)語有什么用
    那么多人贊你
    威威地啦。。。陳先生!!!呵呵
    2008-03-09 23:24 | 陳太

    # re: 使用Future模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]  回復(fù)  更多評論   

    如果能夠在Future中加入Listener就好了,這樣可以在Futher狀態(tài)變化的時(shí)候callback回來,不然每次都去查詢顯示是不合理的
    2008-06-12 10:09 | 傷橋

    # re: 使用Future模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]  回復(fù)  更多評論   

    不僅加上Listener,還需要在Futher中最好加上wait方法,某些必須要等到Futher執(zhí)行完的動作可以在Listener中完成,也可以調(diào)用wait等待,這樣就簡單了
    2008-06-12 10:12 | 傷橋

    # cheap jordan  回復(fù)  更多評論   

    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模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]  回復(fù)  更多評論   

    很好的網(wǎng)站
    2012-11-28 10:55 | wholesale cheap jerseys

    # re: 使用Future模式完成耗時(shí)的背景作業(yè)[JDK1.5 Concurrency]  回復(fù)  更多評論   

    它幫助我很多解決一些問題。其機(jī)會是如此美妙和工作組的麥粒腫,速度如此之快。我認(rèn)為它可能會幫助你的所有。謝謝你。
    2013-02-25 16:36 | write my essay
    主站蜘蛛池模板: 7777久久亚洲中文字幕| 全免费a级毛片免费看无码| 亚洲国产精品线观看不卡| 日韩一级片免费观看| 亚洲午夜久久久久妓女影院 | 亚洲国产精品线观看不卡| 日本最新免费不卡二区在线| 国产伦精品一区二区免费| 日本不卡视频免费| 中文字幕高清免费不卡视频| 日本中文一区二区三区亚洲| 亚洲国产无线乱码在线观看| 久久久久亚洲AV成人网人人网站| 蜜臀98精品国产免费观看| 日韩成人精品日本亚洲| 全黄性性激高免费视频| 久久久久亚洲AV无码去区首| 毛片在线免费视频| 亚洲码欧美码一区二区三区| 亚洲精品无码永久中文字幕| a级毛片免费完整视频| 亚洲AV无码成人网站久久精品大| 好爽…又高潮了毛片免费看| 国产一级淫片a免费播放口| 亚洲91av视频| 亚洲av无码成人精品区在线播放| 国产免费的野战视频| 456亚洲人成影院在线观| 日韩精品亚洲aⅴ在线影院| 韩国二级毛片免费播放| 久久综合给合久久国产免费| 亚洲中文无码a∨在线观看| 又粗又大又黑又长的免费视频| 2020天堂在线亚洲精品专区| 亚洲国产精品无码久久一区二区 | 国产日产亚洲系列| 日本免费网站观看| 四虎免费影院ww4164h| 国产永久免费高清在线| 一级特级aaaa毛片免费观看| 亚洲AV无码国产剧情|