Timer class

Hi, i'm using timer scheduling, i'm usingTimerandTimerTaskclasses. I have one question:

Why is my timer task put on second place inTimerTask[]array of Timer object, even though the first element in a array is empty?

As i understood, Timer object is having an queue where he keeps all timer tasks that are sequentially executed in timer's thread (the queue is of size 128, TimerTask[128]).

The situation is like this: i schedule a timer task for this timer, but this timer is inserted in the second place of this queue:

[0]=null

[1]=NewTimerTask

[2]=null

[3]=null

...

[127]=null

Could anyone please explain me this.

King regards!

Igor

[742 byte] By [igor_ba] at [2007-11-26 18:27:02]
# 1

Why did it put the item on the second element of that array?

Um, because you coded it to do that.

Yep, my crystal ball lets me see the code and that's what it did.

Edit: Ok, so it wasn't your own code afterall but framework code.

Message was edited by:

warnerja

warnerjaa at 2007-7-9 6:01:08 > top of Java-index,Java Essentials,New To Java...
# 2
Most likely you are have some sort of code that states index=1; and then you do timerTaskArray[index]=new TimerTask();.Arrays in Java starts with index = 0;
Lajma at 2007-7-9 6:01:08 > top of Java-index,Java Essentials,New To Java...
# 3

Ok, here is my code... btw i saw TimerTask[128] queue during debuging of Timer object.

Timer timer = new Timer();

task= new someTask ();

timer.scheduleAtFixedRate(task, TASK_DELAY,TASK_PERIOD);

class someTask extends TimerTask {

public void run() {

//do some job...

}

}

where these TASK_DELAY,TASK_PERIOD are constants.

igor_ba at 2007-7-9 6:01:08 > top of Java-index,Java Essentials,New To Java...
# 4

>

> Why is my timer task put on second place in

> TimerTask[] array of Timer object, even though

> the first element in a array is empty?

>

Looking into the source code of TaskQueue shows that they do a ++ assignment before adding the first to the queue. Why? Beats me...

Doesn't it work?

Message was edited by:

Lajm

Lajma at 2007-7-9 6:01:08 > top of Java-index,Java Essentials,New To Java...
# 5

Cheers for the info!

it is working, it was just confusing me why is it put on the second place in TimerTask[] array. It just didn't make any sense... (it still doesn't make much sense, but at least i know who put it on second place in array).

btw...

How can i see the source code of any java class (e.g. TimerQueue class) i.e. to see how some classes from java.lang. package are implemented?

Message was edited by:

igor_b

igor_ba at 2007-7-9 6:01:08 > top of Java-index,Java Essentials,New To Java...
# 6

> Cheers for the info!

>

> it is working, it was just confusing me why is it put

> on the second place in TimerTask[] array. It just

> didn't make any sense... (it still doesn't make much

> sense, but at least i know who put it on second place

> in array).

> btw...

> How can i see the source code of any java class (e.g.

> TimerQueue class) i.e. to see how some classes from

> java.lang. package are implemented?

>

> Message was edited by:

> igor_b

You look into the src.zip-file that comes with the JDK...

I found out that the reason for leaving the first item in an array empty is that it is defensive programming. It comes from the old C-days when you used pointers to point into your arrays. By stepping the pointer once you are aways sure that you are inside the array.

Lajma at 2007-7-9 6:01:08 > top of Java-index,Java Essentials,New To Java...
# 7
Cheers!This was really helpful (for src.zip)...and also thanx for clearing me up this fuzzy stuff about TaskQueue. Now everything makes more sense :)
igor_ba at 2007-7-9 6:01:08 > top of Java-index,Java Essentials,New To Java...