Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java's Timer task guaranteed not to run concurrently?

new Timer(...).schedule(task)

Is task guaranteed to be run by a single thread at any given time?

like image 298
ripper234 Avatar asked Aug 06 '09 09:08

ripper234


2 Answers

From the Javadoc

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.

So, yes, you get a new Thread (separate from the caller's thread). Every task in that timer shares the same thread.

like image 132
Thilo Avatar answered Oct 11 '22 04:10

Thilo


There is a single thread per Timer, so the answer to your question is yes

like image 32
Maurice Perry Avatar answered Oct 11 '22 05:10

Maurice Perry