在特定时间后执行某些代码
我需要在特定时间后执行操作(例如,应用程序启动后 30 分钟,如果应用程序仍在运行)。
我有什么选择?这是否意味着将有一个线程“丢失”等待 30 分钟过去?
理想情况下,在程序启动时,我想做类似以下的事情(有意简化),然后不必再考虑它:
doIfStillUp( 30, new Runnable() {
....
});
那么我应该如何实现 doIfStillUp(...)< /em>?
我应该使用 TimerTask 吗? 执行器框架?
最重要的是(这是为了理解的目的):这是否意味着在 30 分钟内将有一个线程基本上没有空闲而丢失?
如果有一个线程“什么都不做”,这是一个问题吗?如果有 10 000 个线程(我在这里开玩笑)“什么都不做”怎么办?
请注意,我试图了解“大局”,而不是解决特定问题。
I need to execute an action after a specific amount of time (for example 30 minutes after the app started up, if the app is still up).
What are my options and will it necessary means there's going to be one thread "lost" waiting for the 30 minutes to pass by?
Ideally, at program startup, I'd like to do something like the following (simplified on purpose) and then don't have to think about it anymore:
doIfStillUp( 30, new Runnable() {
....
});
So how should I go about implementing doIfStillUp(...)?
Should I use a TimerTask? The Executor framework?
Most importantly (it's for understanding purpose): does this mean there's going to be one thread lost idling for basically nothing during 30 minutes?
If there's going to be one thread "doing nothing", is this an issue? What if there are 10 000 threads (I'm being facetious here) "doing nothing"?
Note that I'm trying to understand the "big picture", not to solve a particular problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Executor 框架是一个合理的选择。
有一个 schedule 方法,只需要一个可运行时间和一个延迟时间。
这非常简单。不一定会有一个线程被阻塞等待您的任务。您可以使用 ScheduledThreadPoolExecutor,如上面链接的那样,使 X 线程准备好运行计划任务。
您可以想象一个保存任务应该运行的时间的数据结构。单个线程可以监视或设置这些延迟,并且有可能在单个线程中监视数千个延迟。当第一次到期时,它将运行该任务。可能使用自己的线程,可能使用线程池中 X 的 1 个。当添加新任务或完成现有任务时,它将等待最早时间到达,然后再次开始整个过程。
The Executor framework is a reasonable choice.
There's a schedule method that just takes a runnable and a delay time.
That's pretty straightforward. There won't necessarily be a thread blocked waiting on your task. You could use a ScheduledThreadPoolExecutor, as linked above that keeps X threads ready to run scheduled tasks.
You can imagine a data structure that holds the time at which a task should be run. A single thread can watch or set up these delays and can potentially watch thousands of them in a single thread. When the first time expires it'll run the task. Potentially using its own thread, potentially using 1 of X in the thread pool. When a new task is added or an existing task is finished it'll wait for the earliest time to arrive and then start the whole process again.
您应该使用计时器。它的 javadoc 回答了您所有的问题。
每个计时器使用一个线程,但计时器按顺序执行多个任务。定时器任务应该很短。如果不是,请考虑使用多个计时器。
当然,如果定时器线程没有任何任务要执行,它就会处于空闲状态。空闲线程不会消耗任何东西(或几乎任何东西),所以我不会担心它。无论如何,你没有太多选择。 10000 个线程什么都不做当然会是一个问题,但这意味着您为每个任务实例化一个计时器,这是错误的。
You should use a Timer. Its javadoc answers all your questions.
One thread is used for every timer, but the timer executes several tasks, sequentially. The timer tasks should be very short. If they aren't, consider using several timers.
Of course, the timer thread will be idle if it doesn't have any task to execute. An idle thread doesn't consume anything (or nearly anything), so I wouldn't worry about it. Anyway, you don't have many choices. 10000 threads doing nothing would of course be an issue, but that would mean that you instantiated one timer per task, which is wrong.
您可以在
java.util.Timer
上安排任务。对于所有计时器任务,java.util.Timer
将创建单个计时器线程。You can schedule task on
java.util.Timer
. For all timer tasks single timer thread will be created byjava.util.Timer
.内置的java计时器是直接的解决方案:
The builtin java timer is the straight away solution: http://download.oracle.com/javase/1,5.0/docs/api/java/util/Timer.html#schedule(java.util.TimerTask, long)