Java Timer TimerTask Example - DigitalOcean
Maybe your like
Java Timer Example
Java Timer class is thread safe and multiple threads can share a single Timer object without need for external synchronization. Timer class uses java.util.TaskQueue to add tasks at given regular interval and at any time there can be only one thread running the TimerTask, for example if you are creating a Timer to run every 10 seconds but single thread execution takes 20 seconds, then Timer object will keep adding tasks to the queue and as soon as one thread is finished, it will notify the queue and another thread will start executing. Java Timer class uses Object wait and notify methods to schedule the tasks. Here is a simple program for Java Timer and TimerTask example.
Notice that one thread execution will take 20 seconds but Java Timer object is scheduled to run the task every 10 seconds. Here is the output of the program:
TimerTask started Timer task started at:Wed Dec 26 19:16:39 PST 2012 Timer task finished at:Wed Dec 26 19:16:59 PST 2012 Timer task started at:Wed Dec 26 19:16:59 PST 2012 Timer task finished at:Wed Dec 26 19:17:19 PST 2012 Timer task started at:Wed Dec 26 19:17:19 PST 2012 Timer task finished at:Wed Dec 26 19:17:39 PST 2012 Timer task started at:Wed Dec 26 19:17:39 PST 2012 Timer task finished at:Wed Dec 26 19:17:59 PST 2012 Timer task started at:Wed Dec 26 19:17:59 PST 2012 Timer task finished at:Wed Dec 26 19:18:19 PST 2012 Timer task started at:Wed Dec 26 19:18:19 PST 2012 TimerTask cancelled Timer task finished at:Wed Dec 26 19:18:39 PST 2012The output confirms that if a task is already executing, Timer will wait for it to finish and once finished, it will start again the next task from the queue. Java Timer object can be created to run the associated tasks as a daemon thread. Timer cancel() method is used to terminate the timer and discard any scheduled tasks, however it doesn’t interfere with the currently executing task and let it finish. If the timer is run as daemon thread, whether we cancel it or not, it will terminate as soon as all the user threads are finished executing. Timer class contains several schedule() methods to schedule a task to run once at given date or after some delay. There are several scheduleAtFixedRate() methods to run a task periodically with certain interval. While scheduling tasks using Timer, you should make sure that time interval is more than normal thread execution, otherwise tasks queue size will keep growing and eventually task will be executing always. That’s all for a quick roundup on Java Timer and Java TimerTask.
Tag » How To Time Things In Java
-
How Do I Time A Method's Execution In Java? - Stack Overflow
-
Measure Elapsed Time In Java - Baeldung
-
Measure Elapsed Time (or Execution Time) In Java - Techie Delight
-
How To Measure Execution Time For A Java Method? - Tutorialspoint
-
How Do I Time Things In Java? - Javamex
-
How To Measure Elapsed Execution Time In Java - Javarevisited
-
Here's How To Calculate Elapsed Time In Java - Stackify
-
How To Measure Time Taken By A Function In Java ? - GeeksforGeeks
-
How To Calculate Method Execution Time In Java | Edureka Community
-
LocalTime (Java Platform SE 8 ) - Oracle Help Center
-
Elegantly Log Elapsed Time In Java | By Hasan Alsulaiman
-
Java Program To Calculate The Execution Time Of Methods - Programiz
-
Java Program To Calculate Difference Between Two Time Periods
-
Working In Java Time - InfoWorld