Is this how to schedule a java method to run 1 second later?

后端 未结 4 692
日久生厌
日久生厌 2021-02-05 17:34

In my method, I want to call another method that will run 1 second later. This is what I have.

final Timer timer = new Timer();

timer.schedule(new TimerTask() {         


        
4条回答
  •  忘了有多久
    2021-02-05 18:06

    Instead of a Timer, I'd recommend using a ScheduledExecutorService

    final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
    
    exec.schedule(new Runnable(){
        @Override
        public void run(){
            MyMethod();
        }
    }, 1, TimeUnit.SECONDS);
    

提交回复
热议问题