使用java.util.Timer [转]

在应用开发中,经常需要一些周期性的操作,比如每5分钟检查一下新邮件等。对于这样的操作最方便、高效的实现方式就是使用java.util.Timer工具类。比如下面的代码每5分钟检查一遍是否有新邮件:

None.gif          private  java.util.Timer timer; 
None.gif        timer 
=   new  Timer( true ); 
ExpandedBlockStart.gifContractedBlock.gif        timer.schedule(
new  java.util.TimerTask()  dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public   void  run()  dot.gif
InBlock.gif                    
// server.checkNewMail(); 检查新邮件 
ExpandedSubBlockEnd.gif
            }
 
ExpandedBlockEnd.gif        }
0 5 * 60 * 1000 ); 

 

使用这几行代码之后,Timer本身会每隔5分钟调用一遍server.checkNewMail()方法,不需要自己启动线程。Timer本身也是多线程同步的,多个线程可以共用一个Timer,不需要外部的同步代码。
    在《The Java Tutorial》中有更完整的例子:

ExpandedBlockStart.gif ContractedBlock.gif public   class  AnnoyingBeep  dot.gif
InBlock.gif    Toolkit toolkit; 
InBlock.gif    Timer timer; 
InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public  AnnoyingBeep()  dot.gif
InBlock.gif    toolkit 
=  Toolkit.getDefaultToolkit(); 
InBlock.gif        timer 
=   new  Timer(); 
InBlock.gif        timer.schedule(
new  RemindTask(), 
InBlock.gif                   
0 ,         // initial delay 
InBlock.gif
                    1 * 1000 );   // subsequent rate 
ExpandedSubBlockEnd.gif
    }
 
InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
class  RemindTask  extends  TimerTask  dot.gif
InBlock.gif    
int  numWarningBeeps  =   3
InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public   void  run()  dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if  (numWarningBeeps  >   0 dot.gif
InBlock.gif            toolkit.beep(); 
InBlock.gif        System.out.println(
" Beep! " ); 
InBlock.gif        numWarningBeeps
--
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
  else   dot.gif
InBlock.gif            toolkit.beep();  
InBlock.gif                System.out.println(
" Time′s up! " ); 
InBlock.gif            
// timer.cancel();  // Not necessary because we call System.exit 
InBlock.gif
            System.exit( 0 );    // Stops the AWT thread (and everything else) 
ExpandedSubBlockEnd.gif
        }
 
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    dot.gif 
ExpandedBlockEnd.gif}
 
None.gif


这段程序,每隔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类也可以方便地用来作为延迟执行,比如下面的代码延迟指定的时间(以秒为单位)执行某操作。类似电视的延迟关机功能。

None.gif dot.gif 
ExpandedBlockStart.gifContractedBlock.gif
public   class  ReminderBeep  dot.gif
InBlock.gif    dot.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public  ReminderBeep( int  seconds)  dot.gif
InBlock.gif    toolkit 
=  Toolkit.getDefaultToolkit(); 
InBlock.gif        timer 
=   new  Timer(); 
InBlock.gif        timer.schedule(
new  RemindTask(), seconds * 1000 ); 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
class  RemindTask  extends  TimerTask  dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public   void  run()  dot.gif
InBlock.gif            System.out.println(
" Time′s up! " ); 
InBlock.gif        toolkit.beep(); 
InBlock.gif        
// timer.cancel();  // Not necessary because we call System.exit 
InBlock.gif
        System.exit( 0 );    // Stops the AWT thread (and everything else) 
ExpandedSubBlockEnd.gif
        }
 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    dot.gif 
ExpandedBlockEnd.gif}
 
None.gif
None.gif


 

95383.html

Lib 2007-01-22 17:43 发表评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值