From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Unlock this course with a free trial
Join today to access over 23,200 courses taught by industry experts.
Delay execution of a Runnable object
From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
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
-
-
-
-
(Locked)
Define and run a Runnable object4m 40s
-
(Locked)
Delay execution of a Runnable object3m 42s
-
Manage background tasks with threads5m 22s
-
(Locked)
Send a message to the UI from a background thread6m 6s
-
(Locked)
Challenge: Run multiple background tasks with threads3m 28s
-
(Locked)
Solution: Run multiple background tasks with threads3m 40s
-
(Locked)
-
-
-
-