How to call a method on specific time in java?

前端 未结 9 1851
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: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);
    

提交回复
热议问题