How do I get my program to sleep for 50 milliseconds?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Pausing execution for a precise duration is a common requirement for rate limiting, polling loops, animation timing, and test synchronization. Every major language provides a sleep or delay primitive, but the API, precision guarantees, and threading implications differ significantly. This article shows how to sleep for 50 milliseconds in Python, JavaScript, Java, C#, Go, C++, and Rust, with notes on precision and best practices.
Python
Python's time.sleep() accepts seconds as a float, so 50 milliseconds is 0.05.
For async Python code, use asyncio.sleep() instead. This releases the event loop during the wait rather than blocking the entire thread.
The time.sleep() call blocks the current thread and cannot be interrupted except by a signal. In GUI applications and async servers, always prefer the non-blocking variant.
JavaScript and Node.js
JavaScript does not have a built-in synchronous sleep. The standard approach wraps setTimeout in a Promise.
In Node.js 16 and above, the timers/promises module provides a built-in promise-based delay.
Never use a busy-wait loop (while (Date.now() < target) {}) to simulate sleep in JavaScript. It blocks the event loop and freezes the entire application.
Java
Java provides Thread.sleep() which accepts milliseconds directly.
Thread.sleep() throws InterruptedException, which you must either catch or declare. For finer control, TimeUnit provides a more readable API.
Inside virtual threads (Project Loom, Java 21+), Thread.sleep() unmounts the virtual thread from the carrier thread, making it non-blocking at the OS level.
C#
C# offers both a blocking sleep and an async delay.
In ASP.NET and UI applications, always prefer Task.Delay over Thread.Sleep. Blocking a thread-pool thread in ASP.NET can lead to thread starvation under load, while blocking the UI thread in a desktop application freezes the interface.
Passing a CancellationToken to Task.Delay lets you interrupt the wait cleanly without waiting for the full duration.
Go
Go's time.Sleep() accepts a time.Duration value.
time.Sleep pauses the current goroutine, not the entire OS thread. The Go runtime scheduler moves other goroutines onto available threads while one sleeps. For cancellable delays, use a context or a timer channel.
C++
C++11 introduced std::this_thread::sleep_for in the <thread> header.
The chrono duration types provide compile-time safety. You cannot accidentally pass seconds when you mean milliseconds because the types are distinct. For POSIX systems, usleep(50000) (microseconds) also works but is considered legacy.
Rust
Rust's standard library provides std::thread::sleep for blocking and tokio::time::sleep for async code.
In async Rust with Tokio, use the non-blocking alternative.
The blocking thread::sleep inside an async runtime starves the executor. Always use the runtime-provided sleep in async contexts.
Common Pitfalls
- Assuming exact precision: Operating system schedulers introduce jitter. A 50 ms sleep may wake after 51-65 ms depending on system load, timer resolution, and power-saving settings.
- Blocking the event loop: In JavaScript and async Python, using synchronous sleep (or a busy-wait loop) blocks the entire event loop and prevents other tasks from executing.
- Ignoring InterruptedException in Java: Swallowing
InterruptedExceptionwith an empty catch block breaks thread interruption contracts and makes graceful shutdown impossible. - Using Thread.Sleep in ASP.NET request handlers: Blocking a thread-pool thread under load causes thread starvation. Use
await Task.Delay()instead to free the thread for other requests. - Busy-waiting instead of sleeping: A
whileloop that repeatedly checks the clock consumes 100% CPU. Always use the platform sleep primitive to yield the processor.
Summary
- Every major language provides a sleep or delay API that accepts a duration in seconds, milliseconds, or a typed duration value.
- Prefer non-blocking, async-aware variants (
asyncio.sleep,Task.Delay,tokio::time::sleep) in event-driven or async applications. - OS scheduler granularity means sleep durations are minimum guarantees, not exact promises.
- Pass cancellation tokens or use context-aware patterns to make delays interruptible for graceful shutdown.
- Never busy-wait as a substitute for sleeping; it wastes CPU resources and degrades system performance.

