How to call a method on specific time in java?

前端 未结 9 1723
Happy的楠姐
Happy的楠姐 2020-11-27 04:43

Is it possible to call a method in java at specific time? For example of I have a piece of code like this:

class Test{

    public static void main(String ar         


        
相关标签:
9条回答
  • 2020-11-27 05:05

    You could use the class Timer

    From documentation:

    A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

    The method schedule(TimerTask task, Date time) is exactly what you want: Schedules the specified task for execution at the specified time.

    If you need schedule in cron format, quartz would be a good solution. (quartz cron like schedule)

    0 讨论(0)
  • 2020-11-27 05:10

    You can use a ScheduledExecutorService, which is "a more versatile replacement for the Timer/TimerTask combination" (according to 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);
    
    0 讨论(0)
  • 2020-11-27 05:14

    Simple demo of 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"));
    
    0 讨论(0)
  • 2020-11-27 05:18

    It is possible. You can schedule a method at some time using Timer and TimerTask.

    For example:

    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);
    

    Refer these links:

    • Timer
    • TimerTask
    0 讨论(0)
  • 2020-11-27 05:25
    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);
    
    0 讨论(0)
  • 2020-11-27 05:26

    If you are talking about SE then the Timer class might be what you are looking for http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html, available since Java 5.

    If you need to time something in an application server context I would recommend having a look at EJB timers http://www.javabeat.net/2007/03/ejb-3-0-timer-services-an-overview/ available since EJB 3.0.

    Alternatively, depending on what you are really trying to do, you could elaborate if using a cron job (or any other OS based timer method) would be more suitable, i.e. you don't want or can't have the VM running all the time.

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