new Timer(...).schedule(task)
task
是否保证在任何给定时间由单个线程运行?
最佳答案
来自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.
所以,是的,您得到了一个新线程(与调用者的线程分开)。该计时器中的每个任务共享同一个线程。
关于java - Java的Timer任务保证不会并发运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1237804/