【问题标题】:How to call a method on specific time in java?如何在java中的特定时间调用方法?
【发布时间】:2012-07-06 11:28:27
【问题描述】:

是否可以在特定时间调用java中的方法?例如我有一段这样的代码:

class Test{

    public static void main(String args[]) {
        // here i want to call foo at : 2012-07-06 13:05:45 for instance
        foo();
    }
}

如何在 java 中做到这一点?

【问题讨论】:

    标签: java


    【解决方案1】:

    使用java.util.Timer 类,您可以创建一个计时器并安排它在特定时间运行。

    示例如下:

    //The task which you want to execute
    private static class MyTimeTask extends TimerTask
    {
    
        public void run()
        {
            //write your code here
        }
    }
    
    public static void main(String[] args) {
    
        //the Date and time at which you want to execute
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = dateFormatter .parse("2012-07-06 13:05:45");
    
        //Now create the time and schedule it
        Timer timer = new Timer();
    
        //Use this if you want to execute it once
        timer.schedule(new MyTimeTask(), date);
    
        //Use this if you want to execute it repeatedly
        //int period = 10000;//10secs
        //timer.schedule(new MyTimeTask(), date, period );
    }
    

    【讨论】:

    【解决方案2】:

    您可以使用ScheduledExecutorService,它是“Timer/TimerTask 组合的更通用替代品”(根据Timer's javadoc):

    long delay = ChronoUnit.MILLIS.between(LocalTime.now(), LocalTime.of(13, 5, 45));
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    scheduler.schedule(task, delay, TimeUnit.MILLISECONDS);
    

    【讨论】:

      【解决方案3】:

      这是可能的,我会使用诸如http://quartz-scheduler.org/之类的库

      Quartz 是一种功能齐全的开源作业调度服务,可以与几乎任何 Java EE 或 Java SE 应用程序集成或一起使用 - 从最小的独立应用程序到最大的电子商务系统。 Quartz 可用于创建简单或复杂的调度程序来执行数十、数百甚至数万个作业;任务被定义为标准 Java 组件的作业,这些组件几乎可以执行任何您可以对其进行编程来执行的操作。

      【讨论】:

        【解决方案4】:

        据我所知,您可以为此使用Quartz-scheduler。我还没用过,但是很多人都推荐给我了。

        【讨论】:

          【解决方案5】:

          你可以使用Timer这个类

          来自文档:

          线程调度任务以供将来执行的工具 后台线程。任务可以安排为一次性执行,或者 定期重复执行。

          schedule(TimerTask task, Date time)方法正是你想要的:调度指定任务在指定时间执行。

          如果您需要cron 格式的日程安排,quartz 将是一个不错的解决方案。 (quartz cron like schedule)

          【讨论】:

            【解决方案6】:

            如果您在谈论 SE,那么 Timer 类可能就是您正在寻找的 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html,自 Java 5 起可用。

            如果您需要在应用程序服务器上下文中计时,我建议您查看自 EJB 3.0 起可用的 EJB 计时器 http://www.javabeat.net/2007/03/ejb-3-0-timer-services-an-overview/

            或者,根据您真正想要做的事情,您可以详细说明使用 cron 作业(或任何其他基于操作系统的计时器方法)是否更合适,即您不想要或不能拥有虚拟机一直在运行。

            【讨论】:

              【解决方案7】:

              java.util.Timer的简单演示

              Timer t=new Timer();
              t.schedule(new TimerTask() {
                  public void run() {
                      foo();
                  }
              }, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-07-06 13:40:20"));
              

              【讨论】:

                【解决方案8】:

                这是可能的。您可以使用TimerTimerTask 在某个时间安排一个方法。

                例如:

                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 10);
                calendar.set(Calendar.MINUTE, 30);
                calendar.set(Calendar.SECOND, 0);
                
                Date alarmTime = calendar.getTime();
                
                Timer _timer = new Timer();
                _timer.schedule(foo, alarmTime);
                

                请参考以下链接:

                【讨论】:

                  【解决方案9】:
                  Timer timer = new Timer();
                  Calendar calendar = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
                  calendar.add(Calendar.SECOND, 5);
                  Date date = calendar.getTime();
                  timer.schedule(new TimerTask() {
                      @Override
                      public void run() {
                          test();
                      }
                  }, date);
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2011-10-06
                    • 2014-08-06
                    • 2020-08-08
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多