Results 1 to 1 of 1
-
How to use Timer class to schedule a task to execute once 5 seconds have passed
This example shows how to use Timer class to schedule a task to execute once 5 seconds have passed.
Java Code:import java.util.Timer; import java.util.TimerTask; /** * Simple demo that uses java.util.Timer to schedule a task to execute once 5 * seconds have passed. */ public class Reminder { Timer timer; public Reminder(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.println("About to schedule task."); new Reminder(5); System.out.println("Task scheduled."); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Cannot get passed these syntax errors
By MrKP in forum New To JavaReplies: 1Last Post: 05-12-2008, 07:05 AM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
Null array when passed to MouseListener
By stevemcc in forum New To JavaReplies: 2Last Post: 04-02-2008, 10:42 PM -
XML parsing with 10sec delay using timer class
By srikanthn in forum XMLReplies: 0Last Post: 02-05-2008, 10:59 AM -
measuring time in nano seconds
By Java Tip in forum Java TipReplies: 0Last Post: 11-06-2007, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks