Constructor Summary | |
---|---|
Timer()
Creates a new timer. |
|
Timer(boolean isDaemon)
Creates a new timer whose associated thread may be specified to run as a daemon. |
|
Timer(String name) Creates a new timer whose associated thread has the specified name. |
|
Timer(String name,
boolean isDaemon) Creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon. |
Method Summary | |
---|---|
void |
cancel()
Terminates this timer, discarding any currently scheduled tasks. |
int |
purge()
Removes all cancelled tasks from this timer's task queue. |
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. |
Constructor Summary | |
---|---|
protected |
TimerTask()
Creates a new timer task. |
Method Summary | |
---|---|
boolean |
cancel()
Cancels this timer task. |
abstract
void |
run()
The action to be performed by this timer task. |
long |
scheduledExecutionTime()
Returns the scheduled execution time of the most recent actual execution of this task. |
import
java.util.Timer; import java.util.TimerTask; public class TimerReminder { Timer timer; public TimerReminder(int seconds) { timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000); } class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); timer.cancel(); //Terminate the timer thread } } public static void main(String args[]) { System.out.format("About to schedule task.%n"); new TimerReminder(4); System.out.println("Task scheduled."); } } |
About
to schedule task. Task scheduled. Time's up! |
import
java.util.Timer; import java.util.TimerTask; /** * Schedule a task that executes once every second. */ public class Beep { Timer timer; public Beep() { timer = new Timer(); timer.schedule(new RemindTask(), 0, //initial delay 1*1000); //subsequent rate } class RemindTask extends TimerTask { int numWarningBeeps = 3; public void run() { if (numWarningBeeps > 0) { System.out.println("Beep!"); numWarningBeeps--; } else { System.out.println("Time's up!"); timer.cancel(); } } } public static void main(String args[]) { System.out.println("About to schedule task."); new Beep(); System.out.println("Task scheduled."); } } |
About
to schedule task. Task scheduled. Beep! Beep! Beep! Time's up! |
import
java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class MyPanel extends java.applet.Applet implements MouseListener { Ball one,two; int choice=1; RemoteInterface remoteClock=null; String host = "Unknown Host"; class MyTimerTask extends TimerTask{ public void run(){ repaint(); } } Timer t; public void init() { addMouseListener(this); one = new Ball(250,250,Color.red); two = new Ball(150,150, Color.blue); try{ host = this.getCodeBase().getHost(); System.out.println("Host is :"+host); remoteClock=(RemoteInterface)java.rmi.Naming.lookup("rmi://192.168.0.141:5099/count"); } catch (Exception x){ System.out.println("Exception!!"+x); x.printStackTrace(); } t= new Timer(); t.schedule(new MyTimerTask(),new Date(System.currentTimeMillis()), 1000); } public void paint(Graphics g) { g.drawString("server: "+host, 10, 10); one.paint(g); two.paint(g); try { g.drawString(""+remoteClock.askTime(), 100,30); } catch (Exception x){ g.drawString("Exception!"+x,10,30); x.printStackTrace(); } } public void mousePressed(MouseEvent e) { int mouseX = e.getX(); int mouseY = e.getY(); if (one.inside(mouseX,mouseY)){ choice=1; } else if (two.inside(mouseX,mouseY)) { choice = 2; } else if (choice == 1) { one.move(mouseX,mouseY); } else if (choice == 2) { two.move(mouseX,mouseY); } repaint(); } public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} } |
jar cvf MyPanel.jar MyPanel.class Ball.class RemoteInterface.class MyPanel$MyTimerTask.class |