简体   繁体   English

在特定时间后执行一些代码

[英]Executing some code after a specific amount of time

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). 我需要在特定时间后执行操作(例如,应用程序启动后30分钟,如果应用程序仍在运行)。

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? 我有什么选择,是否有必要意味着有一个线程“丢失”等待30分钟过去?

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(...) ? 那么我应该如何实现doIfStillUp(...)

Should I use a TimerTask ? 我应该使用TimerTask吗? The Executor framework? Executor框架?

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? 最重要的是(这是为了理解目的):这是否意味着在30分钟内基本上没有任何一个线程会失去空转?

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"? 如果有10 000个线程(我在这里很滑稽)“什么都不做”怎么办?

Note that I'm trying to understand the "big picture", not to solve a particular problem. 请注意,我试图理解“大局”,而不是解决特定问题。

The Executor framework is a reasonable choice. Executor框架是一个合理的选择。

There's a schedule method that just takes a runnable and a delay time. 有一个计划方法,只需要一个可运行和延迟时间。

schedule(Runnable command,
         long delay,
         TimeUnit unit)

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. 您可以使用上面链接的ScheduledThreadPoolExecutor,使X线程准备好运行计划任务。

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. 可能使用自己的线程,可能在线程池中使用1个X. 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. 添加新任务或完成现有任务时,它将等待最早到达的时间,然后再次启动整个过程。

You should use a Timer . 你应该使用一个计时器 Its javadoc answers all your questions. 它的javadoc回答了你所有的问题。

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. 10000个线程无所事事当然是一个问题,但这意味着你为每个任务实例化了一个计时器,这是错误的。

You can schedule task on java.util.Timer . 您可以在java.util.Timer上安排任务。 For all timer tasks single timer thread will be created by java.util.Timer . 对于所有计时器任务,将由java.util.Timer创建单个计时器线程。

内置的java计时器是直接的解决方案: http//download.oracle.com/javase/1,5.0/docs/api/java/util/Timer.html#schedule (java.util.TimerTask,long)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM