Timer实例

先是两个不同的实例

这是swing包中的Timer实例

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.Timer;            //没有该导入,Timer带参的构造方法不能用   
                                     /*而 Timer t=new Timer()却可以用,util包中Timer和swing包中
                                        Timer是不同的*/

import javax.swing.JOptionPane;

public class TimerTest 
{
	public static void main(String[] args)
	{
		TimerPrinter listener=new TimerPrinter();
		Timer t=new Timer(10000,listener);//可以用下面的lambda表达式代替
                /*Timer t=new Timer(10000,event->
                {
                System.out.println("····"+new Date());
                Toolkit.getDefaultkit().beep();
                });*/

t.start();JOptionPane.showMessageDialog(null, "quit program?");System.exit(0);}}class TimerPrinter implements ActionListener{public void actionPerformed(ActionEvent Event) {System.out.println("At the tone,the time is"+new Date());Toolkit.getDefaultToolkit().beep();}}

这是util包中的Timer实例

import java.util.Timer;  
import java.util.TimerTask;  
  
public class M {  
    public static void main(String[] args) {  
 
        Timer timer = new Timer();  
        timer.schedule(new MyTask(), 1000, 2000);  
    }  
}  
  
class MyTask extends TimerTask {  

    public void run() {  
        System.out.println("dddd");  
  
    }  
  
}  

看下API的区别     

这是swing中Timer的介绍

 void    addActionListener(ActionListener listener) 
          Adds an action listener to the Timer.    
protected  void    fireActionPerformed(ActionEvent e) 
          Notifies all listeners that have registered interest for notification on this event type.    
 String    getActionCommand() 
          Returns the string that will be delivered as the action command in ActionEvents fired by this timer.    
 ActionListener[]    getActionListeners() 
          Returns an array of all the action listeners registered on this timer.    
 int    getDelay() 
          Returns the delay, in milliseconds, between firings of action events.    
 int    getInitialDelay() 
          Returns the Timer's initial delay.    
<T extends EventListener> T[]        getListeners(Class<T> listenerType) 
          Returns an array of all the objects currently registered as FooListeners upon this Timer.    
static boolean    getLogTimers() 
          Returns true if logging is enabled.    
 boolean    isCoalesce() 
          Returns true if the Timer coalesces multiple pending action events.    
 boolean    isRepeats() 
          Returns true (the default) if the Timer will send an action event to its listeners multiple times.    
 boolean    isRunning() 
          Returns true if the Timer is running.    
 void    removeActionListener(ActionListener listener) 
          Removes the specified action listener from the Timer.    
 void    restart() 
          Restarts the Timer, canceling any pending firings and causing it to fire with its initial delay.    
 void    setActionCommand(String command) 
          Sets the string that will be delivered as the action command in ActionEvents fired by this timer.    
 void    setCoalesce(boolean flag) 
          Sets whether the Timer coalesces multiple pending ActionEvent firings.    
 void    setDelay(int delay) 
          Sets the Timer's between-event delay, the number of milliseconds between successive action events.    
 void    setInitialDelay(int initialDelay) 
          Sets the Timer's initial delay, the time in milliseconds to wait after the timer is started before firing the first event.    
static void    setLogTimers(boolean flag) 
          Enables or disables the timer log.    
 void    setRepeats(boolean flag) 
          If flag is false, instructs the Timer to send only one action event to its listeners.    
 void    start() 
          Starts the Timer, causing it to start sending action events to its listeners.    
 void    stop() 
          Stops the Timer, causing it to stop sending action events to its listeners.


然后   这是util中Timer的介绍

 void    cancel() 
          Terminates this timer, discarding any currently scheduled tasks.    
 void    schedule(TimerTask task, Date time) 
          Schedules the specified task for execution at the specified time.    
 void    schedule(TimerTask task, Date firstTime, long period) 
          Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.    
 void    schedule(TimerTask task, long delay) 
          Schedules the specified task for execution after the specified delay.    
 void    schedule(TimerTask task, long delay, long period) 
          Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.    
 void    scheduleAtFixedRate(TimerTask task, Date firstTime, long period) 
          Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.    
 void    scheduleAtFixedRate(TimerTask task, long delay, long period) 
          Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值