当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

Timer Task 的简单范例(相当触发器,转自Sun)

Scheduling a Timer Task to Run at a Certain Time
==============================================
Date timeToRun =
   new Date(System.currentTimeMillis()+
   numberOfMillisecondsInTheFuture);
Timer timer = new Timer();
     
timer.schedule(new TimerTask() {
         public void run() {
             // Task here
         }
     }, timeToRun);
============================================

Scheduling a Timer Task to Run Repeatedly

=============================================
// delay for 5 sec.

int delay = 5000;   


// repeat every sec.

int period = 1000;  
Timer timer = new Timer();
     
timer.scheduleAtFixedRate(new TimerTask() {
         public void run() {
             // Task here
         }
}, delay, period);


相关内容
赞助商链接