Pattern: Use of Thread.sleep
instead of delay
Issue: -
A thread can contain multiple coroutines at one time due to coroutines’ lightweight nature, so if one coroutine invokes Thread.sleep
, it could potentially halt the execution of unrelated coroutines and cause unpredictable behavior.
Example of incorrect code:
suspend fun foo() {
Thread.sleep(1_000L)
}
Example of correct code:
suspend fun foo() {
delay(1_000L)
}