在单个线程中调度多个任务
我想让一个线程负责以不同时间间隔安排的多个任务。我想从调度程序中添加和删除任务。有没有任何图书馆可以帮助我解决这个问题。如果没有,我会编写自己的代码,只是不想重新发明轮子。
对于背景,我想向我的应用程序添加警报,由单个线程控制,应用程序的其余部分可以添加/删除计划任务。我可以使用多个 TimerTask 或编写自己的单线程调度程序,但如果有更好的选择,我不想忽视它。
詹姆斯
I want to have a single thread maintain responsibility for multiple tasks scheduled at different intervals. I want to add and remove tasks from the scheduler. Are there any libraries that can help me with this. If not I will code my own just don't want to reinvent the wheel.
For background I want to add alerting to my application, controlled from a single thread where the rest of the application can add/remove scheduled tasks. I can use multiple TimerTask's or write my own single threaded scheduler but if there is better option I don't want to overlook it.
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要 JDK 1.5 或更高版本中开箱即用的东西,您是否查看过 ScheduledExecutorService?
http://download.oracle.com/javase /6/docs/api/java/util/concurrent/ScheduledExecutorService.html
您可以使用以下工厂方法创建其中之一,由单个线程支持:
http://download.oracle.com/javase/6/docs/api/java/util /concurrent/Executors.html#newSingleThreadScheduledExecutor()
还有一个替代工厂方法,它接受 ThreadFactory 作为参数。这使您有机会自定义将在 ScheduledExecutorService 内运行的线程。例如,您可以调用 Thread.setName 为线程指定一个更有意义的名称。这对于调试应用程序非常有帮助。当您生成完整的线程转储时,您将看到附加到线程的自定义名称,而不是 JVM 自动附加的通用名称。
根据需要,调用 Thread.setDaemon(true) 也可能是合适的,这样该线程就不会阻止 JVM 关闭。
此外,最佳实践是在使用完 ExecutorService 后,通过调用 ExecutorService.shutdown 或 ExecutorService.shutdownNow 来清理它。如果没有保证调用关闭(例如在finally块中),则可能会在应用程序中引入线程泄漏错误。从您描述的用法来看,这听起来不太可能会咬您,但当我向某人推荐使用 ExecutorService 时,我总是喜欢强调这一点。从 JavaDocs 中很容易忽略这一点。
If you need something that's available right out of the box in JDK 1.5 or later, have you looked at ScheduledExecutorService?
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html
You can create one of these, backed by a single thread, using this factory method:
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newSingleThreadScheduledExecutor()
There's also an alternative factory method that accepts a ThreadFactory as an argument. This gives you an opportunity to customize the Thread that will run inside the ScheduledExecutorService. For example, you can call Thread.setName to give the thread a more meaningful name. This is very helpful for debugging an application. When you generate a full thread dump, you'll see your custom name attached to the thread instead of a generic name attached automatically by the JVM.
Depending on the need, it might also be appropriate to call Thread.setDaemon(true), so that this thread won't block JVM shutdown.
Also, it's best practice to clean up any ExecutorService after you're done with it by calling ExecutorService.shutdown or ExecutorService.shutdownNow. Without a guaranteed call to shutdown (such as in a finally block), it's possible to introduce a thread leak bug in your application. From the usage you describe, it sounds unlikely that this will bite you, but I always like to stress this when I give someone a recommendation to use an ExecutorService. It's easy to miss this point from the JavaDocs.
如果你想要一个基本的库,你可以使用内置的 ScheduledExecutorService。这允许添加重复任务并取消它们。您可以将其设置为单线程。
If you want a basic library, you can use the built in ScheduledExecutorService. This allows to add repeating tasks and cancel them. You can set it to be single threaded.
查看 Quartz。它几乎是 Java 中任务调度的首选。如果您浏览文档,您可以将其配置为单线程。
Check out Quartz. It's pretty much the go-to for task scheduling in Java. You can configure it to be single-threaded if you poke through the docs.