Class of the Month: Control Event Executions with the New Timer Class Maine

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

Paradigm Technologies
(207) 680-2123
179 Main St Ste 402
Waterville, ME
C D I Inc
(207) 628-2842
Kingfield Rd N
New Portland, ME
Quaid Donald & Associates
(207) 725-0808
288 Mere Point Rd
Brunswick, ME
Eaton Mountain Data Services
(207) 293-9355
Kimball Pd-Cross Rd
Vienna, ME
Banctec
(207) 278-5649
223 Pratt Rd
Newport, ME
Office Solutions ME
207-319-6045
Portland Rd
Gray, ME
Computer Software Consultants
(207) 947-1120
249 15th St
Bangor, ME
Infoharbor
(207) 865-3994
Freeport, ME
Phil Brown Associates Inc
(207) 871-7044
758 Main St
South Portland, ME
Quadic Systems Inc
(207) 773-2662
39 Darling Ave Ste 3
South Portland, ME

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

Featured Local Company

Office Solutions ME

207-319-6045
Portland Rd
Gray, ME

Related Local Events
Fitness Ball Workout
Dates: 5/7/2010 - 5/7/2009
Location: WALDO COUNTY GENERAL HOSPITAL'S EDUCATION CENTER
BELFAST, ME
View Details

Strong Women Workout
Dates: 12/17/2009 - 12/17/2009
Location: WALDO COUNTY GENERAL HOSPITAL'S EDUCATION CENTER
BELFAST, ME
View Details

Fitness Ball Workout
Dates: 12/10/2009 - 12/10/2009
Location: WALDO COUNTY GENERAL HOSPITAL'S EDUCATION CENTER
BELFAST, ME
View Details

Fitness Ball Workout
Dates: 12/8/2009 - 12/8/2009
Location: WALDO COUNTY GENERAL HOSPITAL'S EDUCATION CENTER
BELFAST, ME
View Details

Strong Women Workout
Dates: 12/1/2009 - 12/1/2009
Location: WALDO COUNTY GENERAL HOSPITAL'S EDUCATION CENTER
BELFAST, ME
View Details

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