Search code examples
javaandroidmultithreadingrunnableandroid-handler

Handler.postDelayed v/s Runnable.run. Is it alright to call .run instead of .postDelayed?


I was trying to implement a looping Runnable. The example I've found seems to use the following idea to kick-start the runnable.

        handler = new Handler();
        final Runnable r = new Runnable() {
            public void run() {
                handler.postDelayed(this, 10000);
                doIt();
                count ++;
            }
        };
        r.run();// what I prefer
//      handler.postDelayed(r, 1000);//their idea

I prefer using the call to the run() method to start the Runnable. What would be the possible troubles that I could get into if any by a direct call to run()!

Thanks! :)


Solution

  • I prefer using the call to the run() method to start the Runnable.

    OK.

    What would be the possible troubles that I could get into if any by a direct call to run()!

    The first pass through run() would happen immediately, as opposed to your commented-out code, which would cause the first pass through run() to occur ~1000ms from now.

    However, I would dump the Handler. postDelayed() is also a method on View, so just use some widget in your UI.