posts - 28,  comments - 13,  trackbacks - 0

          在應用開發(fā)中,經(jīng)常需要一些周期性的操作,比如每5分鐘檢查一下新郵件等。對于這樣的操作最方便、高效的實現(xiàn)方式就是使用java.util.Timer工具類。比如下面的代碼每5分鐘檢查一遍是否有新郵件:

          ???????? private ?java.util.Timer?timer;?
          ????????timer?
          = ? new ?Timer( true );?
          ????????timer.schedule(
          new ?java.util.TimerTask()? {?
          ????????????
          public ? void ?run()? {?
          ????????????????????
          // server.checkNewMail();?檢查新郵件?
          ????????????}
          ?
          ????????}
          ,? 0 ,? 5 * 60 * 1000 );?

          ?

          使用這幾行代碼之后,Timer本身會每隔5分鐘調(diào)用一遍server.checkNewMail()方法,不需要自己啟動線程。Timer本身也是多線程同步的,多個線程可以共用一個Timer,不需要外部的同步代碼。
          ??? 在《The Java Tutorial》中有更完整的例子:

          public ? class ?AnnoyingBeep? {?
          ????Toolkit?toolkit;?
          ????Timer?timer;?
          ??
          ????
          public ?AnnoyingBeep()? {?
          ????toolkit?
          = ?Toolkit.getDefaultToolkit();?
          ????????timer?
          = ? new ?Timer();?
          ????????timer.schedule(
          new ?RemindTask(),?
          ???????????????????
          0 ,???????? // initial?delay?
          ??????????????????? 1 * 1000 );?? // subsequent?rate?
          ????}
          ?
          ??
          ????
          class ?RemindTask? extends ?TimerTask? {?
          ????
          int ?numWarningBeeps? = ? 3 ;?
          ??
          ????????
          public ? void ?run()? {?
          ????????
          if ?(numWarningBeeps? > ? 0 )? {?
          ????????????toolkit.beep();?
          ????????System.out.println(
          " Beep! " );?
          ????????numWarningBeeps
          -- ;?
          ????????}
          ? else ? {?
          ????????????toolkit.beep();??
          ????????????????System.out.println(
          " Time′s?up! " );?
          ????????????
          // timer.cancel();? // Not?necessary?because?we?call?System.exit?
          ????????????System.exit( 0 );??? // Stops?the?AWT?thread?(and?everything?else)?
          ????????}
          ?
          ????????}
          ?
          ????}
          ?
          ?????
          }
          ?


          這段程序,每隔3秒響鈴一聲,并打印出一行消息。循環(huán)3次。程序輸出如下:
          Task scheduled.
          Beep!?????
          Beep!????? //one second after the first beep
          Beep!????? //one second after the second beep
          Time′s up! //one second after the third beep

          Timer類也可以方便地用來作為延遲執(zhí)行,比如下面的代碼延遲指定的時間(以秒為單位)執(zhí)行某操作。類似電視的延遲關機功能。

          ?
          public ? class ?ReminderBeep? {?
          ?????
          ????
          public ?ReminderBeep( int ?seconds)? {?
          ????toolkit?
          = ?Toolkit.getDefaultToolkit();?
          ????????timer?
          = ? new ?Timer();?
          ????????timer.schedule(
          new ?RemindTask(),?seconds * 1000 );?
          ????}
          ?
          ??
          ????
          class ?RemindTask? extends ?TimerTask? {?
          ????????
          public ? void ?run()? {?
          ????????????System.out.println(
          " Time′s?up! " );?
          ????????toolkit.beep();?
          ????????
          // timer.cancel();? // Not?necessary?because?we?call?System.exit?
          ????????System.exit( 0 );??? // Stops?the?AWT?thread?(and?everything?else)?
          ????????}
          ?
          ????}
          ?
          ?????
          }
          ?


          ?

          posted on 2007-01-22 17:43 Lib 閱讀(758) 評論(0)  編輯  收藏 所屬分類: Java
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345



          我的JavaEye博客
          http://lib.javaeye.com


          常用鏈接

          留言簿(2)

          隨筆分類

          文章分類

          FLASH

          Java

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 翼城县| 赤峰市| 海林市| 河池市| 桃源县| 台东县| 潍坊市| 新丰县| 康定县| 绿春县| 梅州市| 西安市| 堆龙德庆县| 赤峰市| 门头沟区| 故城县| 健康| 通渭县| 永州市| 巴东县| 清徐县| 柳江县| 怀宁县| 盐津县| 胶州市| 大荔县| 和龙市| 安龙县| 翁牛特旗| 靖边县| 沧源| 永修县| 南阳市| 麦盖提县| 饶河县| 南漳县| 镇平县| 台山市| 永顺县| 栖霞市| 绥芬河市|