隨筆-72  評(píng)論-20  文章-0  trackbacks-1

          一個(gè)簡單的Java 計(jì)時(shí)器框架實(shí)現(xiàn)


          本文介紹一個(gè)簡單的基于線程機(jī)制的計(jì)時(shí)器的實(shí)現(xiàn)。 將把 java.util.Timer 和 java.util.TimerTask 統(tǒng)稱為 Java 計(jì)時(shí)器框架,它們使程序員可以很容易地計(jì)劃簡單的任務(wù)。
          計(jì)劃框架建立在 Java 定時(shí)器框架類的基礎(chǔ)之上。因此,在解釋如何使用計(jì)劃框架以及如何實(shí)現(xiàn)它之前,我們將首先看看如何用這些類進(jìn)行計(jì)劃。
          想象一個(gè)煮蛋計(jì)時(shí)器,在數(shù)分鐘之后(這時(shí)蛋煮好了)它會(huì)發(fā)出聲音提醒您。下例中的代碼構(gòu)成了一個(gè)簡單的煮蛋計(jì)時(shí)器的基本結(jié)構(gòu):
          EggTimer.java
           
          import java.util.Timer;
          import java.util.TimerTask;
           
          public class EggTimer {
              private final Timer timer = new Timer();
           
              private final int minutes;
           
              public EggTimer(int minutes) {
                   this.minutes = minutes;
              }
           
              public void start() {
                   timer.schedule(new TimerTask() {
                     public void run() {
                        playSound();
                        timer.cancel();
                     }
           
                     private void playSound() {
                          System.out.println("Your egg is ready!");
                          // Start a new thread to play a sound...
                     }
                 }, minutes * 60 * 1000);
              }
           
              public static void main(String[] args) {
                   EggTimer eggTimer = new EggTimer(2);
                   eggTimer.start();
              }
          }
          EggTimer 實(shí)例擁有一個(gè) Timer 實(shí)例,用于提供必要的計(jì)劃。用 start() 方法啟動(dòng)煮蛋計(jì)時(shí)器后,它就計(jì)劃了一個(gè) TimerTask ,在指定的分鐘數(shù)之后執(zhí)行。時(shí)間到了, Timer 就在后臺(tái)調(diào)用 TimerTask 的 start() 方法,這會(huì)使它發(fā)出聲音。在取消計(jì)時(shí)器后這個(gè)應(yīng)用程序就會(huì)中止。

          java.util.Timer實(shí)現(xiàn)定時(shí)刪除信息線程

           
          public class AlarmTimer {
                 static Logger log=Logger.getLogger(AlarmTimer.class);
                 private Timer timer = new Timer();
                 private int minutes;
                 private int type = VIRGIN;
                 private String id;
                 private Object o;
                 private static Map timerQuery;
                 public static final int VIRGIN = 0;
                 public static final int CALL_STATUS = 1;
                 public static final int CALL_SMS = 2;
                 public static final int CALL_MMS = 3;
                 public static final int UPDATE_LOCATION = 4;
                 public static final int DUR_LOCATION = 5;
                 static {
                        timerQuery = new ConcurrentHashMap();
                 }
           
                 public AlarmTimer() {}
           
                 public AlarmTimer(int minutes, int type, Object o, String id) {
                        this.minutes = minutes;
                        this.type = type;
                        this.o = o;
                        this.id = id;
                 }
           
                 @SuppressWarnings("unchecked")
                 public void start() {
                        timerQuery.put(id, timer);
                        timer.schedule(new TimerTask() {
                               public void run() {
                                      if (type == 1) {
                                             delStatus((ConcurrentHashMap) o, id);
                                             timer.cancel();
                                      } else if (type == 2) {
                                             delSms((SmsBean) o, id);
                                             timer.cancel();
                                      } else if (type == 3) {
                                             delMms((MmsBean) o, id);
                                             timer.cancel();
                                      } else if (type == 4) {
                                             updateLocation((ConcurrentHashMap) o, id);
                                             timer.cancel();
                                      } else if (type == 5){
                                             duraLocation ((ConcurrentHashMap) o, id);
                                             timer.cancel();
                                      }
                               }
           
                               private void delStatus(ConcurrentHashMap hMap, String RegId) {
                                      hMap.remove(RegId);
                                      log.info(RegId+ " status removed!");
                               }
           
                               private void delSms(SmsBean smsBean, String RegId) {
                                      try {
                                             Set keys = timerQuery.keySet();
                                             Iterator e = keys.iterator();
                                             while (e.hasNext()) {
                                                    String temp = (String) e.next();
                                                    if (temp.equals(RegId)) {
                                                           smsBean.deleteBysmsId(RegId);
                                                           timerQuery.remove(id);
                                                           log.info(RegId+ " sms removed!");
                                                    }
                                             }
                                      } catch (Exception e) {
                                             e.printStackTrace();
                                      }
                               }
           
                               private void delMms(MmsBean mmsBean, String RegId) {
                                      try {
                                             Set keys = timerQuery.keySet();
                                             Iterator e = keys.iterator();
                                             while (e.hasNext()) {
                                                    String temp = (String) e.next();
                                                    if (temp.equals(RegId)) {
                                                           mmsBean.deleteBymmsId(RegId);
                                                          timerQuery.remove(id);
                                                           log.info(RegId+ " mms removed!");
                                                    }
                                             }
                                             String TMP_PATH = Messages.getString("FILE_PATH");
                                             String userName = mmsBean.getRegId();
                                             String fileFullName = mmsBean.getFileUri();
                                             String toDel = TMP_PATH + "/" + userName + "/"
                                                           + fileFullName;
                                             File file = new File(toDel);
                                             if (file.delete()) {
                                                    log.info(RegId+ " mms file removed!");
                                             } else {
                                                    log.warn(RegId+ " mms file does not exist!");
                                             }
                                      } catch (Exception e) {
                                             e.printStackTrace();
                                      }
                               }
                              
                         private void updateLocation(ConcurrentHashMap hMap, String RegId) {
                                      Notification notiEvent = null;
                                      if (hMap.containsKey(RegId)) {
                                             notiEvent = (Notification) hMap.get(RegId);
                                             String num = notiEvent.getAddress();
                                             int frequency = notiEvent.getFrequency();
                                             Location loc = new Location();
                                             try {
                                              double latitude = loc.queryLatitude(num);
                                              double longitude = loc.queryLongtitude(num);
                                      DateFormater dFormater = new DateFormater(new Date());
                                              String timeStamp = dFormater.dateFormat();
                                              notiEvent.setLatitude(latitude);
                                              notiEvent.setLongitude(longitude);
                                              notiEvent.setTimeStamp(timeStamp);
                                              log.info(RegId+ " location update!");
                                              AlarmTimer aTimer = new AlarmTimer(frequency,
                                                    AlarmTimer.UPDATE_LOCATION, hMap, RegId);
                                              aTimer.start();
                                              // ---just for test show---
                                              log.info(RegId+ " latitude update:" + latitude + " at:"
                                                            + timeStamp);
                                               log.info(RegId+ " longitude update:" + longitude + " at:"
                                                            + timeStamp);
                                               // ------------------------
                                             } catch (Exception e) {
                                                    e.printStackTrace();
                                             }
                                      }
                               }
                              
                         private void duraLocation(ConcurrentHashMap hMap,String RegId){
                                      hMap.remove(RegId);
                                      AlarmTimer aTimer=new AlarmTimer();
                                      aTimer.delTimer(RegId);
                                      LocationBean lBean=new LocationBean();
                                      try {
                                             lBean.delete(RegId);
                                      } catch (Exception e) {
                                             e.printStackTrace();
                                      }
                                      log.info(RegId+ " location service removed!");
                               }
                              
                        }, minutes * 60 * 1000);
                 }
           
                 public void delTimer(String regId) {
                        this.id = regId;
                        Set keys = timerQuery.keySet();
                        Iterator e = keys.iterator();
                        while (e.hasNext()) {
                               String temp = (String) e.next();
                               if (temp.equals(id)) {
                                      timer = (Timer) timerQuery.get(temp);
                                      timerQuery.remove(id);
                                      timer.cancel();
                                      log.info(id+"Timer removed!");
                               }
                        }
                 }
          }
           
          1.構(gòu)造子設(shè)計(jì)
          public AlarmTimer(int minutes, int type, Object o, String id) {
                        this.minutes = minutes;
                        this.type = type;
                        this.o = o;
                        this.id = id;
          }
          由于傳入的數(shù)據(jù)結(jié)構(gòu)類型不同,所以統(tǒng)一為Object類型,然后根據(jù)type類型定義,將其向下轉(zhuǎn)行為具體類型。
           
          2.start()方法
          start()方法體利用Timer類實(shí)現(xiàn)計(jì)時(shí)器線程。通過定義匿名內(nèi)部類TimerTask來實(shí)現(xiàn)具體類型操作。cancel()起到銷毀線程的作用。
           
          3.timerQuery設(shè)計(jì)
          設(shè)計(jì)timerQuery目的是記錄所有啟動(dòng)的新線程的句柄。這樣如果主動(dòng)銷毀線程(而非在指定的時(shí)間,即比指定時(shí)間早的情況下),可以檢查此表,獲得對(duì)象句柄(參見delTimer方法)。
           
          4.匿名類TimerTask類中方法分析
          delStatus/delSms/delMms功能類似,即從相應(yīng)的數(shù)據(jù)存儲(chǔ)體中刪除數(shù)據(jù),并在timerQuery中刪除該線程的句柄。
          updateLocation方法的目的是每隔一段時(shí)間來更新數(shù)據(jù)存儲(chǔ)體中的數(shù)據(jù)。具體實(shí)現(xiàn)方法是在指定時(shí)間得到線程讀出它的id,并更新它的數(shù)據(jù)。啟動(dòng)一個(gè)新的線程將數(shù)據(jù)保存在新線程中,原線程銷毀。如此往復(fù),直到在Map中找不到此id的線程。
          duraLocation方法的目的是在指定的時(shí)間銷毀Map中的線程。



          posted on 2007-08-19 05:12 前方的路 閱讀(594) 評(píng)論(0)  編輯  收藏 所屬分類: Java技術(shù)
          主站蜘蛛池模板: 呼图壁县| 新蔡县| 寿宁县| 澎湖县| 罗甸县| 八宿县| 武陟县| 从江县| 隆林| 榆中县| 涞源县| 建瓯市| 蒙自县| 纳雍县| 长寿区| 泸西县| 民丰县| 精河县| 神木县| 陈巴尔虎旗| 开化县| 安阳县| 汉寿县| 枞阳县| 霍林郭勒市| 唐山市| 民勤县| 高青县| 望都县| 裕民县| 苏州市| 屯昌县| 定陶县| 杭锦旗| 济南市| 黄梅县| 大关县| 伊吾县| 宝山区| 抚顺县| 光泽县|