Run a program or method at specific time in Java

后端 未结 4 1725
忘掉有多难
忘掉有多难 2020-12-21 20:46

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.

相关标签:
4条回答
  • 2020-12-21 21:20

    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.

    0 讨论(0)
  • 2020-12-21 21:23

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

    0 讨论(0)
  • 2020-12-21 21:31

    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.

    0 讨论(0)
  • 2020-12-21 21:32

    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.

    0 讨论(0)
提交回复
热议问题