Java的Timer任务是否保证不会同时运行? [英] Is Java's Timer task guaranteed not to run concurrently?

查看:328
本文介绍了Java的Timer任务是否保证不会同时运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

任务保证在任何给定时间由单个线程运行吗?

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

推荐答案

来自 Javadoc


对应于每个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.

所以,是的,你得到一个新的线程(与调用者的线程分开)。该计时器中的每个任务共享同一个线程。

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

这篇关于Java的Timer任务是否保证不会同时运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆