tags:

views:

266

answers:

1

In a java class i have two timers

TimerTask t1 = new TimerTask() {.. } TimerTask t2 = new TimerTask() { ...}

Do t1 and t2 execute as two separate threads, how do you verify it

+3  A: 

You need to put each TimeTask in a Timer, which will spawn off the thread. From the API manual for Timer:

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.

Paul Tomblin