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 閱讀(766) 評論(0)  編輯  收藏 所屬分類: Java
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456



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


          常用鏈接

          留言簿(2)

          隨筆分類

          文章分類

          FLASH

          Java

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 江山市| 化州市| 巴东县| 无极县| 阿拉善右旗| 会泽县| 满洲里市| 甘孜| 三门峡市| 杭锦旗| 中宁县| 景宁| 库尔勒市| 沙洋县| 峨眉山市| 定边县| 龙川县| 保定市| 临海市| 耒阳市| 华蓥市| 工布江达县| 巴楚县| 玉屏| 信宜市| 东港市| 泽州县| 平南县| 祁东县| 沧州市| 普宁市| 巨鹿县| 东山县| 墨玉县| 丽水市| 吉安县| 金塔县| 诸城市| 驻马店市| 从江县| 富顺县|