Class of the Month: Control Event Executions with the New Timer Class Rhode Island

We begin a new column on useful Java classes by looking at Timer and TimerTask, which facilitate execution of code at a predetermined time or during specified time intervals.

Local Companies

Genesys Consulting Associates
(401) 351-0050
936 Branch Ave
Providence, RI
Hathaway Center
(401) 467-5890
95 Hathaway St
Providence, RI
Smith Donald M
(401) 823-4740
8 Birchwood Ln
West Warwick, RI
Kyran Research Associates Inc
(401) 849-7734
127 John Clarke Rd
Middletown, RI
Sea Corporation
(401) 847-2260
62 Johnny Cake Hill Rd
Middletown, RI
Tekniquest Systems Inc
(401) 245-7372
580 Maple Ave
Barrington, RI
Rudnick Computer Consultants
(401) 272-9262
220 5th St
Providence, RI
Systems Resource Management Inc
(401) 849-2913
42 Valley Rd
Middletown, RI
Prosoft
(401) 848-0134
1272 W Main Rd
Middletown, RI
Labyrinth Business Solutions Llc
(401) 843-8700
40 Mary St
Newport, RI

provided by: 
Originally published at Internet.com


Amidst all the changes and additions in JDK 1.3, was a small but useful class called Timer. Timer and TimerTask both belong to the java.util package. They facilitate execution of code at a predetermined time or during specified time intervals. In Unix, you can use the "at" command to schedule a process to run at specific times. A similar facility exists under NT servers. There are, however, times when you need to schedule execution of a particular task within a Java program.

Piroz Mohseni

Let's look at a very simple example.

The class ttask inherits from TimerTask and implements a single run() method. Similar to threads, the code inside of the run() method is what is executed on a scheduled basis. In our case, we simply print out a message. Another method that you can implement is cancel(), which stops the task. You only need to call this method once, because after an event is cancelled, it doesn't make sense to cancel it again. You won't get an error, however, if you repeatedly call cancel. It just won't do anything.

Finally, the last method of TimerTask is scheduledExecutionTime(). It returns the scheduled execution time of the task. You can use this value to decide whether you should skip execution or take other actions. If your application requires scheduling and fixed-time execution of tasks, you should take a look at Timer and TimerTask classes...

Read article at Internet.com site
Regional Articles
- Class of the Month: Control Event Executions with the New Timer Class Bristol RI
- Class of the Month: Control Event Executions with the New Timer Class Central Falls RI
- Class of the Month: Control Event Executions with the New Timer Class Coventry RI
- Class of the Month: Control Event Executions with the New Timer Class Cranston RI
- Class of the Month: Control Event Executions with the New Timer Class Cumberland RI
- Class of the Month: Control Event Executions with the New Timer Class East Greenwich RI
- Class of the Month: Control Event Executions with the New Timer Class East Providence RI
- Class of the Month: Control Event Executions with the New Timer Class Johnston RI
- Class of the Month: Control Event Executions with the New Timer Class Lincoln RI
- Class of the Month: Control Event Executions with the New Timer Class Middletown RI
- Class of the Month: Control Event Executions with the New Timer Class Narragansett RI
- Class of the Month: Control Event Executions with the New Timer Class Newport RI
- Class of the Month: Control Event Executions with the New Timer Class North Kingstown RI
- Class of the Month: Control Event Executions with the New Timer Class North Providence RI
- Class of the Month: Control Event Executions with the New Timer Class Pawtucket RI
- Class of the Month: Control Event Executions with the New Timer Class Providence RI
- Class of the Month: Control Event Executions with the New Timer Class Tiverton RI
- Class of the Month: Control Event Executions with the New Timer Class Warwick RI
- Class of the Month: Control Event Executions with the New Timer Class West Warwick RI
- Class of the Month: Control Event Executions with the New Timer Class Westerly RI
- Class of the Month: Control Event Executions with the New Timer Class Woonsocket RI

Topics: 
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML