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

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.

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
Related Articles
Regional Articles
- Class of the Month: Control Event Executions with the New Timer Class Alabama
- Class of the Month: Control Event Executions with the New Timer Class Alaska
- Class of the Month: Control Event Executions with the New Timer Class Arizona
- Class of the Month: Control Event Executions with the New Timer Class Arkansas
- Class of the Month: Control Event Executions with the New Timer Class California
- Class of the Month: Control Event Executions with the New Timer Class Colorado
- Class of the Month: Control Event Executions with the New Timer Class Connecticut
- Class of the Month: Control Event Executions with the New Timer Class DC
- Class of the Month: Control Event Executions with the New Timer Class Delaware
- Class of the Month: Control Event Executions with the New Timer Class Florida
- Class of the Month: Control Event Executions with the New Timer Class Georgia
- Class of the Month: Control Event Executions with the New Timer Class Hawaii
- Class of the Month: Control Event Executions with the New Timer Class Idaho
- Class of the Month: Control Event Executions with the New Timer Class Illinois
- Class of the Month: Control Event Executions with the New Timer Class Indiana
- Class of the Month: Control Event Executions with the New Timer Class Iowa
- Class of the Month: Control Event Executions with the New Timer Class Kansas
- Class of the Month: Control Event Executions with the New Timer Class Kentucky
- Class of the Month: Control Event Executions with the New Timer Class Louisiana
- Class of the Month: Control Event Executions with the New Timer Class Maine
- Class of the Month: Control Event Executions with the New Timer Class Maryland
- Class of the Month: Control Event Executions with the New Timer Class Massachusetts
- Class of the Month: Control Event Executions with the New Timer Class Michigan
- Class of the Month: Control Event Executions with the New Timer Class Minnesota
- Class of the Month: Control Event Executions with the New Timer Class Mississippi
- Class of the Month: Control Event Executions with the New Timer Class Missouri
- Class of the Month: Control Event Executions with the New Timer Class Montana
- Class of the Month: Control Event Executions with the New Timer Class Nebraska
- Class of the Month: Control Event Executions with the New Timer Class Nevada
- Class of the Month: Control Event Executions with the New Timer Class New Hampshire
- Class of the Month: Control Event Executions with the New Timer Class New Jersey
- Class of the Month: Control Event Executions with the New Timer Class New Mexico
- Class of the Month: Control Event Executions with the New Timer Class New York
- Class of the Month: Control Event Executions with the New Timer Class North Carolina
- Class of the Month: Control Event Executions with the New Timer Class North Dakota
- Class of the Month: Control Event Executions with the New Timer Class Ohio
- Class of the Month: Control Event Executions with the New Timer Class Oklahoma
- Class of the Month: Control Event Executions with the New Timer Class Oregon
- Class of the Month: Control Event Executions with the New Timer Class Pennsylvania
- Class of the Month: Control Event Executions with the New Timer Class Rhode Island
- Class of the Month: Control Event Executions with the New Timer Class South Carolina
- Class of the Month: Control Event Executions with the New Timer Class South Dakota
- Class of the Month: Control Event Executions with the New Timer Class Tennessee
- Class of the Month: Control Event Executions with the New Timer Class Texas
- Class of the Month: Control Event Executions with the New Timer Class Utah
- Class of the Month: Control Event Executions with the New Timer Class Vermont
- Class of the Month: Control Event Executions with the New Timer Class Virginia
- Class of the Month: Control Event Executions with the New Timer Class Washington
- Class of the Month: Control Event Executions with the New Timer Class West Virginia
- Class of the Month: Control Event Executions with the New Timer Class Wisconsin
- Class of the Month: Control Event Executions with the New Timer Class Wyoming

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

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