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

android, java

Solution

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

Problem

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() { public void run() { MyMethod(); Log.w("General", "This has been called one second later"); timer.cancel(); } }, 1000); ``` Is this how it's supposed to be done? Are there other ways to do it since I'm on Android? Can it be repeated without any problems?

Original source

Related problems