Simple function that execute an instruction every X time until Y condition is accomplished What do you think about this implementation?

inline fun every(duration: Long, timeUnit: TimeUnit, whileCondition: () -&gt; Boolean = { true }, function: () -&gt; Unit) {
    while (whileCondition()) {
        function()
        Thread.sleep(timeUnit.toMillis(duration))
    }
}


This allows calling the following statement:

every(5, TimeUnit.SECONDS){
    println("Hello world")
}
Usually it's a bad idea to put to sleep whole thread. I'd recommend use Java Timer or kotlin's wrapper for it.

Here is an example:

fixedRateTimer("default", false, 0L, 1000){
    println("Hello!")
    if (theEndIsNear) cancel()
}