Search code examples
javaandroidwait

Java how to wait before going on


I want my runnable to wait a couple of seconds before starting again, any advice on how to do this?

if i use this.wait(); it doesn't work because that stops everthing in my program and i just want the runnable to pause.


Solution

  • Here is an example for 2 second delayed Runnable

    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // Do stuff
    
            handler.postDelayed(this, 2000);
        }
    };
    
    handler.postDelayed(runnable, 2000);