Sleep
The sleep function pauses the current thread for a specified duration, during which other threads can continue running. The parameter type is Duration, which can use time units like Duration.second and Duration.millisecond.
Duration supports common operations: multiplication (e.g., 200 * Duration.millisecond), addition, subtraction, etc. If dur <= Duration.Zero, the thread only yields execution resources without actually sleeping.
The example on the right is a countdown program: after printing each remaining second, it sleeps for 200 milliseconds, and finally prints "go". Through sleeping, you can observe the effects of alternating thread execution.
Note
The actual sleep time may be slightly longer than the specified duration because thread wakeup also requires scheduler involvement.