posts - 28,  comments - 13,  trackbacks - 0

          在應用開發中,經常需要一些周期性的操作,比如每5分鐘檢查一下新郵件等。對于這樣的操作最方便、高效的實現方式就是使用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分鐘調用一遍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秒響鈴一聲,并打印出一行消息。循環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類也可以方便地用來作為延遲執行,比如下面的代碼延遲指定的時間(以秒為單位)執行某操作。類似電視的延遲關機功能。

          ?
          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 閱讀(574) 評論(0)  編輯  收藏 所屬分類: Java
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345



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


          常用鏈接

          留言簿(2)

          隨筆分類

          文章分類

          FLASH

          Java

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 博白县| 石景山区| 依安县| 卓尼县| 兴文县| 望城县| 古浪县| 扶沟县| 昌宁县| 长海县| 平邑县| 罗田县| 盐边县| 民勤县| 昂仁县| 吉隆县| 额敏县| 湾仔区| 白银市| 元阳县| 柳州市| 吴江市| 汽车| 博白县| 闽清县| 施秉县| 瑞昌市| 时尚| 嘉祥县| 敖汉旗| 尼木县| 博野县| 罗定市| 中宁县| 唐河县| 扶风县| 湾仔区| 西盟| 台南市| 秀山| 沙湾县|