KT.SLEEP_INSTEAD_OF_DELAY

Usages of 'Thread.sleep' in suspending functions and coroutine blocks

Report usages of Thread.sleep in suspending functions and coroutine blocks. 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.

Noncompliant Code

Copy
suspend fun foo() {
    Thread.sleep(1_000L)
}

Compliant Code

Copy
suspend fun foo() {
    delay(1_000L)
}

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html