java Timer TimerTask 多线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12015642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java Timer TimerTask multiple threads
提问by Majid Laissi
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:
我正在使用 Timer 和 TimerTask 来长时间轮询聊天应用程序的新消息。我想研究两种“稍微”不同的可能性:
1 :Timer declared as local variable
1:定时器声明为局部变量
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.
*问题:每次调用该方法时,我都可以看到创建了一个新线程,[Timer-1]、[Timer-2] 等。并且在 Eclipse 调试窗口中,即使在getLastMessages之后,它们似乎都在运行(..)完成运行并向客户端返回一个值。如果计时器实际上使用线程,这可能会导致一个巨大的问题,并且在很少的事务之后服务器最终会消耗所有的机器资源。
2 :Timer declared as local field
2 :定时器声明为本地字段
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) ?
*问题:每次调用该方法时,都使用相同的线程 [Thread-1],但我不确定是否连续两次调用,后者将取消/覆盖前者(该类是 @Autowired by spring ) ?
Any suggestions ? Thank you.
有什么建议 ?谢谢你。
采纳答案by Tudor
Here is the source codeof the schedule
method:
下面是该方法的源代码schedule
:
190 public void schedule(TimerTask task, long delay) {
191 if (delay < 0)
192 throw new IllegalArgumentException("Negative delay.");
193 sched(task, System.currentTimeMillis()+delay, 0);
194 }
and the sched
method:
和sched
方法:
386 private void sched(TimerTask task, long time, long period) {
387 if (time < 0)
388 throw new IllegalArgumentException("Illegal execution time.");
389
390 // Constrain value of period sufficiently to prevent numeric
391 // overflow while still being effectively infinitely large.
392 if (Math.abs(period) > (Long.MAX_VALUE >> 1))
393 period >>= 1;
394
395 synchronized(queue) {
396 if (!thread.newTasksMayBeScheduled)
397 throw new IllegalStateException("Timer already cancelled.");
398
399 synchronized(task.lock) {
400 if (task.state != TimerTask.VIRGIN)
401 throw new IllegalStateException(
402 "Task already scheduled or cancelled");
403 task.nextExecutionTime = time;
404 task.period = period;
405 task.state = TimerTask.SCHEDULED;
406 }
407
408 queue.add(task);
409 if (queue.getMin() == task)
410 queue.notify();
411 }
412 }
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 mainLoop
method in the file, you can see that it takes tasks from the queue one-by-one ordered by their schedule time and executes them.
从这里你可以清楚地看到队列被用来在内部存储任务,这意味着后面的任务不会覆盖前面的任务。如果你还检查mainLoop
文件中的方法,你可以看到它从队列中按照他们的调度时间一个一个排序的任务并执行它们。
So it should not be a problem to schedule multiple tasks on the same Timer
object.
所以在同一个Timer
对象上调度多个任务应该不成问题。
As a side-note, consider replacing Timer
with ScheduledThreadPoolExecutor
available since Java 1.5.
作为一个侧面说明,考虑更换Timer
与ScheduledThreadPoolExecutor
从Java 1.5中提供。