I’m using Timer and TimerTask to long poll for new messages for a chat application.
I would like to examine two “slightly” different possibilities:
1 : Timer declared as local variable
public List<MessageBean> getLastMessages(...) {
[...]
Timer timer = new Timer(true); //**Timer declared as local variable**
while (someCondiction) {
MessagesTimerTask returnMessagesTask = new MessagesTimerTask([...]);
timer.schedule(returnMessagesTask, 6000);
synchronized (listMessageBean) {
listMessageBean.wait();
//notify is called in the MessagesTimerTask which extends TimerTask
}
}
}
*Problem: Every time i call the method, i can see that a new thread is created, [Timer-1], [Timer-2], etc.. And in Eclipse Debug window they all seem to be running even after the getLastMessages(..) finishes running and returns a value to the client.
This may cause a huge problem if the timers are actually using threads, and after few transactions the server will eventually consume all the machine resources.
2 : Timer declared as local field
private final Timer timer = new Timer(true); //**Timer declared as local field**
public List<MessageBean> getLastMessages(...) {
[...]
while (someCondiction) {
MessagesTimerTask returnMessagesTask = new MessagesTimerTask([...]);
timer.schedule(returnMessagesTask, 6000);
synchronized (listMessageBean) {
listMessageBean.wait();
//notify is called in the MessagesTimerTask which extends TimerTask
}
}
}
*Problem: every time i call the method, the same thread is used [Thread-1], but i’m not sure if i make two consecutive calls, the latter will cancel/override the former (the class is @Autowired by spring) ?
Any suggestions ?
Thank you.
Here is the source code of the
schedulemethod:and the
schedmethod:From here you can clearly see that a queue is being used to store tasks internally, meaning that later tasks will not overwrite the earlier ones. If you also check the
mainLoopmethod in the file, you can see that it takes tasks from the queue one-by-one ordered by their schedule time and executes them.So it should not be a problem to schedule multiple tasks on the same
Timerobject.As a side-note, consider replacing
TimerwithScheduledThreadPoolExecutoravailable since Java 1.5.