是Java的计时器任务确保不同时运行? [英] Is Java's Timer task guarenteed not to run concurrently?

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

问题描述

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的计时器任务确保不同时运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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