随笔-72  评论-20  文章-0  trackbacks-1

一个简单的Java 计时器框架实现


本文介绍一个简单的基于线程机制的计时器的实现。 将把 java.util.Timer 和 java.util.TimerTask 统称为 Java 计时器框架,它们使程序员可以很容易地计划简单的任务。
计划框架建立在 Java 定时器框架类的基础之上。因此,在解释如何使用计划框架以及如何实现它之前,我们将首先看看如何用这些类进行计划。
想象一个煮蛋计时器,在数分钟之后(这时蛋煮好了)它会发出声音提醒您。下例中的代码构成了一个简单的煮蛋计时器的基本结构:
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 实例拥有一个 Timer 实例,用于提供必要的计划。用 start() 方法启动煮蛋计时器后,它就计划了一个 TimerTask ,在指定的分钟数之后执行。时间到了, Timer 就在后台调用 TimerTask 的 start() 方法,这会使它发出声音。在取消计时器后这个应用程序就会中止。

java.util.Timer实现定时删除信息线程

 
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.构造子设计
public AlarmTimer(int minutes, int type, Object o, String id) {
              this.minutes = minutes;
              this.type = type;
              this.o = o;
              this.id = id;
}
由于传入的数据结构类型不同,所以统一为Object类型,然后根据type类型定义,将其向下转行为具体类型。
 
2.start()方法
start()方法体利用Timer类实现计时器线程。通过定义匿名内部类TimerTask来实现具体类型操作。cancel()起到销毁线程的作用。
 
3.timerQuery设计
设计timerQuery目的是记录所有启动的新线程的句柄。这样如果主动销毁线程(而非在指定的时间,即比指定时间早的情况下),可以检查此表,获得对象句柄(参见delTimer方法)。
 
4.匿名类TimerTask类中方法分析
delStatus/delSms/delMms功能类似,即从相应的数据存储体中删除数据,并在timerQuery中删除该线程的句柄。
updateLocation方法的目的是每隔一段时间来更新数据存储体中的数据。具体实现方法是在指定时间得到线程读出它的id,并更新它的数据。启动一个新的线程将数据保存在新线程中,原线程销毁。如此往复,直到在Map中找不到此id的线程。
duraLocation方法的目的是在指定的时间销毁Map中的线程。



posted on 2007-08-19 05:12 前方的路 阅读(575) 评论(0)  编辑  收藏 所属分类: Java技术

只有注册用户登录后才能发表评论。


网站导航: