先是两个不同的实例
这是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.