Reputation: 57
I am using timer class to poll my database continuously and after that will do long operation whether scheduleAtFixedRate
will create new thread of execution if the current processor is not completed with in the specified interval.
public void contextInitialized(ServletContextEvent sce) {
TreamisTransportSMS t = new TreamisTransportSMS();
t.transport();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 1000, 1000);
System.gc();
}
Upvotes: 1
Views: 2147
Reputation: 55897
The Timer documentaion says:
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, no, only one execution thread
In response to your supplementary question about creating your own worker thread.
Your MyTimerTask could do anything it wishes, including starting a new thread. You then have to manage housekeeping of that thread, and must consider the possibility that you could be creating an escalating number of threads. My guess is that the designers of Timer are trying to protect you from such escalation.
Upvotes: 2