If you do not have a basic understanding of Java, this tutorial is not for you. Some programming knowledge and object-oriented concepts are required. Very trivial things are left unexplained.
You will need the following imports:
Or just import the whole util package.
The Timer class allows you to do two important things:
The idiom is to first instantiate a Timer and then schedule TimerTasks to be run by the timer according to certain rules.
The timer's schedule method is used to preform both of the functions listed above. Here are the method signatures with descriptions (taken straight from the API):
I will demonstrate the third and fourth (the first and second are just the same, except instead of taking a number of milliseconds to wait, they take a Date in the future at which to begin running).
Here I declare the TimerTask as an anonymous class. You can also do something like
The following code is similar, except it schedules a timer to be run multiple times:
Note that the cancel() called here if called on the TimerTask and not the Timer. This will cancel only that particular task. Timer also has a method called cancel() which cancels all TimerTasks bound to a timer.
To achieve the same result using the methods taking dates, just pass in a Date:
There are also these methods to schedule on Timers:
These act exactly like the other method using delay and repeat interval, but will change the repeat interval to compensate for lost time.
For example, if garbage collecting locks up the computer for 200 ms, and the task is set to run every 1000 ms, it will wait less than 1000 ms so that the task always runs at the same interval rather than with the same wait. If you've ever looked at an old server source in server.java, this is the same general concept with subtracting processing time from the cycling time.
Here is a demo class to play with:
And sample output:
P.S.: you can also add ActionListeners to timers and they will trigger whenever tasks are executed. If you use anonymous classes be careful of the scope - only final variables can be accessed. It's better to just use method calls.
You will need the following imports:
Code: [Select]
import java.util.Timer;
import java.util.TimerTask;Or just import the whole util package.
The Timer class allows you to do two important things:
- Set up a task to run at some point in the future
- Set up a task to repeat at a certain interval (e.g. every 500 milliseconds)
The idiom is to first instantiate a Timer and then schedule TimerTasks to be run by the timer according to certain rules.
Code: [Select]
Timer timer = new Timer();The timer's schedule method is used to preform both of the functions listed above. Here are the method signatures with descriptions (taken straight from the API):
Code: [Select]
void schedule(TimerTask task, Date time)
Schedules the specified task for execution at the specified time.
void schedule(TimerTask task, Date firstTime, long period)
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
void schedule(TimerTask task, long delay)
Schedules the specified task for execution after the specified delay.
void schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.I will demonstrate the third and fourth (the first and second are just the same, except instead of taking a number of milliseconds to wait, they take a Date in the future at which to begin running).
Code: [Select]
public static void demoDelayedTask() {
Timer timer = new Timer();
int delayTime = 2000; // Milliseconds to wait before running delayed task
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Delayed task has been run.");
System.out.println("Current time: " + System.currentTimeMillis());
}
}, delayTime);
}Here I declare the TimerTask as an anonymous class. You can also do something like
Code: [Select]
TimerTask task = new TimerTask() {...};if it's more convenient.The following code is similar, except it schedules a timer to be run multiple times:
Code: [Select]
public static void demoRepeatingTask() {
Timer timer = new Timer();
int delayTime = 3000; // Time to wait before starting the timer
int repeatInterval = 300; // Time to wait between runs
timer.schedule(new TimerTask() {
int timesRun;
@Override
public void run() {
timesRun++;
System.out.println("Repeating task run " + timesRun + " times.");
printTime();
if(timesRun >= 10) {
System.out.println("Run 10 times, it's time to stop!");
cancel();
}
}
}, delayTime, repeatInterval);
}Note that the cancel() called here if called on the TimerTask and not the Timer. This will cancel only that particular task. Timer also has a method called cancel() which cancels all TimerTasks bound to a timer.
To achieve the same result using the methods taking dates, just pass in a Date:
Code: [Select]
Date dateToRun = new Date(System.currentTimeMillis() + timeToWait);is an example of a date to pass. The date replaces the delay time.The other fields are the same. Using Dates is useful for stuff like scheduling something in hours/days/months using GregorianCalendar rather than something in the near future using milliseconds.There are also these methods to schedule on Timers:
Code: [Select]
void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
void scheduleAtFixedRate(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.These act exactly like the other method using delay and repeat interval, but will change the repeat interval to compensate for lost time.
For example, if garbage collecting locks up the computer for 200 ms, and the task is set to run every 1000 ms, it will wait less than 1000 ms so that the task always runs at the same interval rather than with the same wait. If you've ever looked at an old server source in server.java, this is the same general concept with subtracting processing time from the cycling time.
Here is a demo class to play with:
Code: [Select]
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Aftermath
* Demonstrates scheduling a task to run in the future.
*/
public class TimerDemo {
public static void main(String... args) throws Exception {
System.out.println("Starting Timer demo.");
printTime();
System.out.println("Demonstrating delayed task: ");
demoDelayedTask();
// Timer tasks are kept track of in separate threads.
// Without this line the other timer would begin and
// the output would overlap. This is just for display purposes.
Thread.sleep(5000);
System.out.println("Demonstrating delayed repeating task: ");
demoRepeatingTask();
}
/**
* Schedules a task to run in a certain period of time.
*/
public static void demoDelayedTask() {
Timer timer = new Timer();
int delayTime = 2000; // Milliseconds to wait before running delayed task
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Delayed task has been run.");
System.out.println("Current time: " + System.currentTimeMillis());
}
}, delayTime);
}
/**
* Schedules a repeating task to begin in a certain period of time.
* Also demonstrates how to stop a task after a certain number of runs.
*/
public static void demoRepeatingTask() {
Timer timer = new Timer();
int delayTime = 3000; // Time to wait before starting the timer
int repeatInterval = 300; // Time to wait between runs
timer.schedule(new TimerTask() {
int timesRun;
@Override
public void run() {
timesRun++;
System.out.println("Repeating task run " + timesRun + " times.");
printTime();
if(timesRun >= 10) {
System.out.println("Run 10 times, it's time to stop!");
cancel();
}
}
}, delayTime, repeatInterval);
}
//
public static void printTime() {
System.out.println("Current time: " + System.currentTimeMillis());
}
}And sample output:
Code: [Select]
Starting Timer demo.
Current time: 1290055585097
Demonstrating delayed task:
Delayed task has been run.
Current time: 1290055587099
Demonstrating delayed repeating task:
Repeating task run 1 times.
Current time: 1290055593099
Repeating task run 2 times.
Current time: 1290055593399
Repeating task run 3 times.
Current time: 1290055593699
Repeating task run 4 times.
Current time: 1290055593999
Repeating task run 5 times.
Current time: 1290055594299
Repeating task run 6 times.
Current time: 1290055594599
Repeating task run 7 times.
Current time: 1290055594899
Repeating task run 8 times.
Current time: 1290055595199
Repeating task run 9 times.
Current time: 1290055595499
Repeating task run 10 times.
Current time: 1290055595799
Run 10 times, it's time to stop!P.S.: you can also add ActionListeners to timers and they will trigger whenever tasks are executed. If you use anonymous classes be careful of the scope - only final variables can be accessed. It's better to just use method calls.

















great tutorial! Runescape guide also find free rs accounts and items!