2013-07-25 66 views
0

我需要強制我的Java應用程序在特定的日期和時間執行一些操作。場景如下:如何在特定日期和時間運行特定方法?

1- user set a specific time and frequency (every day, every month) 
2- system starts a trigger for the request 

3- once that pre-defined frequency and time are reached 
3.1 - system performs the required actions that are kept in a method 

我發現了這個answer,但無法使其工作。

一個例子將不勝感激。

回答

2

您可以使用Executor從JDK本身,這裏是打完折例如:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); 
ScheduledFuture<?> handle scheduler.scheduleAtFixedRate(new Runnable() { 
      public void run() { System.out.println("your code is here :)"); } 
     }, 1, 100, TimeUnit.MINUT); 

所以這個代碼開始1分鐘後運行並運行後100分鐘。

爲了以後取消,你會從here

handle.cancel(true)

閱讀

相關問題