https://kotlinlang.org logo
#android
Title
# android
o

Ofir Bar

01/09/2020, 8:15 PM
hey guys, I know it sounds stupid but why don’t I get ANR? I am blocking the main thread for 10 seconds. I also tried Thread.sleep(10000)
🤔 1
h

Himanshu Sharma

01/09/2020, 8:21 PM
ANR usually occurs when the app doesn’t respond to User interaction for more than n seconds. here you are blocking the main thread but probably not trying to intact with the app button click, view tap, scroll, swipe etc
o

Ofir Bar

01/09/2020, 8:34 PM
I tried what you suggested with the following code, but it still doesn’t crash:
a

Anastasia Finogenova

01/09/2020, 10:07 PM
Try actual interactions aka click on the screen
p

Pablichjenkov

01/09/2020, 11:22 PM
delay()
does not block the thread. It pretty much schedule a Job to be executed at the specified time.
👆 2
It suspend the coroutine, whatever code block inside the runBlocking lambda, but it does not touch the underlaying thread.
r

Romain Saury

01/10/2020, 9:58 AM
But
runBlocking
should block the thread no ? 🤔
k

Kris Wong

01/10/2020, 2:04 PM
run blocking uses an event loop based dispatcher
i would still expect Thread.sleep to block the main thread, however
r

Romain Saury

01/10/2020, 3:27 PM
The dispatcher used for the coroutine block shouldn’t change that runBlocking is supposed to block the thread until the block completion. Or am I missing something ?
k

Kris Wong

01/10/2020, 3:37 PM
it does block. an event loop also blocks the main thread.
runBlocking
blocks in the same way. it continues to process events on the event loop until the work is completed.
2 Views