Run a program or method at specific time in Java

吃可爱长大的小学妹 提交于 2019-12-29 09:30:08

问题


I just want that my program or method should run at specific date and time. i have heard about Timer and TimerTask in java API. But don't know exactly how to use it.


回答1:


If you want to run a java program at a specific time you probably want to look at the OS tools (like cron or at).

If you want to run a method inside of an already running java application then the ScheduleExecutorService, while it may be overkill, is pretty easy to use.




回答2:


If this is for your own benefit and not for a project I would suggest you look into http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html which is a replacement for java.util.Timer. If however you want a robust scheduler, I concur with the previous posters with regards to Quartz.




回答3:


Maybe you should use a third-party library with a higher level API like Quartz and use the SimpleTrigger.




回答4:


You can run your task inside a Glassfish Java EE server. It supports a Timer Service that fires background tasks at specified intervals. When you're running a cluster of Glassfish servers on different machines, they'll collaborate to fire the task exactly once.

A simpler approach is to rely on cron for Unix systems. At specified times you can run your java task via the java command.

I've also used pycron on Windows, which is a service that emulates cron.



来源:https://stackoverflow.com/questions/2488736/run-a-program-or-method-at-specific-time-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!