From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Delay execution of a Runnable object

Delay execution of a Runnable object

- [Instructor] A handler object's post function adds an operation to the end of the current thread stack and executes it immediately when all the other operations are done. But, you can delay a posted operation in a couple of different ways. The easiest way is to call a function named postDelayed. It looks like this. I'll change this call of handler.post to handler.postDelayed. And I'm going to wrap these parenthesis around the lambda expression. Then I'll pass in a value of 1000. I'll run that code on my device. I immediately see my synchronous operations and then one second later, I see the runnable operation executed. I'll come back here, and I'm going to duplicate this a couple of times, and then I'll run the code again. I'll clear the log and run the code, and now I see all three operations are executed at the same time. And this is an important aspect of working with handlers and runnables. The value that…

Contents