- Get link
- X
- Other Apps
I am a Java Programmer who has just suffered enough from the pain of using java.util.Timer and java.util.TimerTask. I would like to share some of my experiences about it. :)
This tutorial assumes that you have the basic knowledge about JAVA.
~~~~~

Just like what you can see in the name,
java.util.Timer is really like a timer that will countdown time to execute single task repeatedly, have a look on the api for more info.
java.util.TimerTask is the task that is to be executed, have a look on the api for more info.
If you take a look in java.util.Timer's api, you would find out two different constructor:
1st:
Timer anyNameForTimer = new Timer();
or
2nd:
Timer anyNameForTimer = new Timer(boolean isDaemon);
In case you do not understand their differences, the first one is the timer that will only execute after everything has finish executed, but the 2nd one will terminate after it ends.
~~~~~
This tutorial assumes that you have the basic knowledge about JAVA.
~~~~~
Just like what you can see in the name,
java.util.Timer is really like a timer that will countdown time to execute single task repeatedly, have a look on the api for more info.
java.util.TimerTask is the task that is to be executed, have a look on the api for more info.
If you take a look in java.util.Timer's api, you would find out two different constructor:
1st:
Timer anyNameForTimer = new Timer();
or
2nd:
Timer anyNameForTimer = new Timer(boolean isDaemon);
In case you do not understand their differences, the first one is the timer that will only execute after everything has finish executed, but the 2nd one will terminate after it ends.
TimerTask is a bit different.
As TimerTask is an abstract class,
you must inherits (extends) it to create the task you want.
~~~~~
The basic things you have to know about Timer is:
1) timer can be scheduled by using:
timer.schedule(TimerTask, delay(int), period(int));
timer.schedule(TimerTask, delay(int), period(int));
2) timer can be cancelled using timer.cancel();
3) TimerTask can be cancelled also using timerTask.cancel();
(assume timer is an instance of Timer, timerTask is an instance of TimerTasks's subclass
~~~~~
So, there was a homework for me that requires me to use timer,
I realized that my timer caused my program to keep running even if
it had reached the end of line.
I changed the constructor to the 2nd one to force it to be stopped,
without understanding why the timer did not want to be stopped.
I want to write a code that will reschedule the timer after the TimerTask has executed,
what I have done is to create a new Timer to schedule it.
This is my prev code:
class RDTResender extends TimerTask {
DataPacket dataToResend;
RDTResender(DataPacket original){
dataToResend = original;
}
public void run() {
try {
udt.send(dataToResend);
//just assume that I have declared:
//delay: int //resend: RDTResender(The TimerTask subclass) //timer: Timer
delay *= 2;
resend.cancel();
resend = new RDTResender(dataToResend);
timer.cancel();
timer = new Timer(true);
timer.schedule(resend, delay, delay);
} catch (IOException e){
return;
}
}
}
There are something fishy here:
I cancel my Timer inside the timer then create a new one in order to
set up a new timer for different time.
I thought cancelling the timer will force everything to be cancelled, well it is actually not.
Note that: Timer can only schedule one task at a time,
if they are two tasks are scheduling at a time for two different timers,
or if it is cancelled,
then the program will be crashed, throwing the following exception:
IllegalStateException
Well, doing this way is certainly not clean and nice.
~~~~~
The difference between:
timer.cancel() and timerTask.cancel() is:
timer.cancel() cancels every unscheduled task in the timer but WOULD NOT
delete the scheduling task in the timer.
timerTask.cancel() cancels the single task from executing anymore.
The time when I was writing my prev code, I did not fully understand the difference,
and therefore used timer.cancel() which caused me a lot of troubles later.
~~~~~
So, after I understood the difference of timer.cancel() and timerTask.cancel()
So this time, I would just use timerTask.cancel() instead of calling
timer.cancel() also because it is not necessary and
not good to have:
a timer execute a timerTask to cancel the timer and reopen a new one,
sounds fishy right?
I write something like this:
class RDTResender extends TimerTask {
DataPacket dataToResend;
RDTResender(DataPacket original){
dataToResend = original;
}
public void run() {
try {
udt.send(dataToResend);
//delay *= 2;
//delay * 2
//causes the program
//running too slowly
delay += 1;
resend.cancel();
resend = new RDTResender(dataToResend);
//timer.cancel();
//timer = new Timer(true);
timer.schedule(resend, delay, delay);
} catch (IOException e){
return;
}
}
}
Please note that I do not cancel timer this time, I just reschedule it.
Now the program is running smoothly because it will not have
conflicts of scheduling task.
~~~~~
Take home message:
1) Try not to cancel your timer in TimerTask, it will make your program much more complicated.
2) We should cancel only the TimerTask, if you only want to reschedule your timer and you are
sure that the timer is only running one task.
3) Use ONE timer will be good if we can make sure that we only need to cancel one task. HOWEVER,
sometimes timer.cancel() is necessary.
p.s.: this problem is nice to be read as well.
If you want a better class that can schedule multiple tasks,
you might want to consider this class: ScheduledThreadPoolExecutor
Other reference: http://javarevisited.blogspot.sg/2013/02/what-is-timer-and-timertask-in-java-example-tutorial.html
Thank you for reading this blog!
Feel free to give any comments!
Thank you.
pharaoh
30/3/2014
8:16a.m.
So, there was a homework for me that requires me to use timer,
I realized that my timer caused my program to keep running even if
it had reached the end of line.
I changed the constructor to the 2nd one to force it to be stopped,
without understanding why the timer did not want to be stopped.
I want to write a code that will reschedule the timer after the TimerTask has executed,
what I have done is to create a new Timer to schedule it.
This is my prev code:
class RDTResender extends TimerTask {
DataPacket dataToResend;
RDTResender(DataPacket original){
dataToResend = original;
}
public void run() {
try {
udt.send(dataToResend);
//just assume that I have declared:
//delay: int //resend: RDTResender(The TimerTask subclass) //timer: Timer
delay *= 2;
resend.cancel();
resend = new RDTResender(dataToResend);
timer.cancel();
timer = new Timer(true);
timer.schedule(resend, delay, delay);
} catch (IOException e){
return;
}
}
}
There are something fishy here:
I cancel my Timer inside the timer then create a new one in order to
set up a new timer for different time.
I thought cancelling the timer will force everything to be cancelled, well it is actually not.
Note that: Timer can only schedule one task at a time,
if they are two tasks are scheduling at a time for two different timers,
or if it is cancelled,
then the program will be crashed, throwing the following exception:
IllegalStateException
Well, doing this way is certainly not clean and nice.
~~~~~
The difference between:
timer.cancel() and timerTask.cancel() is:
timer.cancel() cancels every unscheduled task in the timer but WOULD NOT
delete the scheduling task in the timer.
timerTask.cancel() cancels the single task from executing anymore.
The time when I was writing my prev code, I did not fully understand the difference,
and therefore used timer.cancel() which caused me a lot of troubles later.
~~~~~
So, after I understood the difference of timer.cancel() and timerTask.cancel()
So this time, I would just use timerTask.cancel() instead of calling
timer.cancel() also because it is not necessary and
not good to have:
a timer execute a timerTask to cancel the timer and reopen a new one,
sounds fishy right?
I write something like this:
class RDTResender extends TimerTask {
DataPacket dataToResend;
RDTResender(DataPacket original){
dataToResend = original;
}
public void run() {
try {
udt.send(dataToResend);
//delay *= 2;
//delay * 2
//causes the program
//running too slowly
delay += 1;
resend.cancel();
resend = new RDTResender(dataToResend);
//timer.cancel();
//timer = new Timer(true);
timer.schedule(resend, delay, delay);
} catch (IOException e){
return;
}
}
}
Please note that I do not cancel timer this time, I just reschedule it.
Now the program is running smoothly because it will not have
conflicts of scheduling task.
~~~~~
Take home message:
1) Try not to cancel your timer in TimerTask, it will make your program much more complicated.
2) We should cancel only the TimerTask, if you only want to reschedule your timer and you are
sure that the timer is only running one task.
3) Use ONE timer will be good if we can make sure that we only need to cancel one task. HOWEVER,
sometimes timer.cancel() is necessary.
p.s.: this problem is nice to be read as well.
If you want a better class that can schedule multiple tasks,
you might want to consider this class: ScheduledThreadPoolExecutor
Other reference: http://javarevisited.blogspot.sg/2013/02/what-is-timer-and-timertask-in-java-example-tutorial.html
Thank you for reading this blog!
Feel free to give any comments!
Thank you.
pharaoh
30/3/2014
8:16a.m.
Comments
Post a Comment