简体   繁体   English

Java中多个任务的任务调度?

[英]Task scheduling for multiple tasks in java?

i have build a java clock with using timer,which works fine for a single task to alarm on next given/setted time, but i am having problem in scheduling multiple tasks(alarms for diff. times) with this timer, as two times can clash each other(same times for two different works) how to synchronize between such conditions, please help.... 我已经使用计时器构建了一个java时钟,它适用于单个任务在下一个给定/设置的时间发出警报,但我在使用此计时器调度多个任务(差异时间的警报)时遇到问题,因为两次可以相互冲突(两个不同作品的同一时间)如何在这些条件之间同步,请帮助....

Thanks and Regards 谢谢并恭祝安康

Alok Sharma Alok Sharma

I'm not sure what you're trying to do, but if you use quartz scheduler, you can resolve just about any scheduling/synchronisation task: 我不确定您要做什么,但是如果您使用石英调度程序,则可以解决几乎所有调度/同步任务:

http://www.quartz-scheduler.org/ http://www.quartz-scheduler.org/

I agree with Lukas that you can use quartz. 我同意Lukas你可以使用石英。 It is the best, scalable and robust solution. 这是最好的,可伸缩的和强大的解决方案。

But if you need something relatively small you can continue using timer based solution. 但如果您需要相对较小的东西,您可以继续使用基于计时器的解决方 As javadoc of Timer class indicates your tasks should take very few time. 由于Timer类的javadoc表明您的任务应该花费很少的时间。 In this case you can forget about time clash. 在这种情况下,你可以忘记时间冲突。 If your tasks take more then 0.1 seconds run them in separate thread. 如果您的任务花费了0.1秒以上的时间,请在单独的线程中运行它们。 I mean use Timer as a trigger that just makes task to start in separate thread. 我的意思是使用Timer作为触发器,只是让任务在单独的线程中启动。

The thread may be done as following: 该线程可以按如下方式完成:

  1. Create thread yourself. 自己创建线程。 If you are in J2EE container it is bad practice. 如果你在J2EE容器中,这是不好的做法。 If you are in Tomcat it is ... not so bad. 如果您在Tomcat中,那还不错。
  2. Use thread pool. 使用线程池。 Comments about container are relevant here too. 关于容器的注释在这里也很重要。
  3. Use JMS: Timer just pushes message to JMS. 使用JMS:Timer只是将消息推送到JMS。 MDB or its equivalent receives message and performs task. MDB或其等效项接收消息并执行任务。

Using Timer itsef in J2EE container is a bad practice too. 在J2EE容器中使用Timer itsef也是一种不好的做法。 If you are there and wish to be "clean" use JCA to to run Timer. 如果你在那里并希望“干净”使用JCA来运行Timer。

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

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