Skip to content

daemon thread

A daemon thread is a background thread that a program doesn’t wait on before it exits. The runtime keeps running while at least one ordinary foreground thread is still active, but once only daemon threads remain, the program terminates and stops them abruptly.

The timeline below runs a small program with a foreground main() thread and two background workers. Flipping a worker between foreground and daemon shows when the program exits and which threads it stops mid-task:

Interactive diagram — enable JavaScript to view.

The name borrows from a daemon, a long-lived background process that handles routine work without direct user interaction. A daemon thread fills that role inside a single process, running open-ended tasks such as health checks, log flushing, or cache expiry that should never delay shutdown. Its counterpart is an ordinary foreground thread, sometimes called a user thread, which must finish before the program can end.

Stopping a thread at shutdown has a cost. Its cleanup code may never run, so open files, network connections, and database transactions can be left in an inconsistent state. Work that must complete belongs on a foreground thread with an explicit stop signal.

Runtimes that offer thread-based concurrency expose the choice as a per-thread flag. In Python, you pass daemon=True to the threading module’s Thread constructor, and on the JVM, you call setDaemon(true). Both runtimes want the flag set before the thread starts and raise an error if you set it afterward.

An Intro to Threading in Python

Tutorial

An Intro to Threading in Python

In this intermediate-level tutorial, you'll learn how to use threading in your Python programs. You'll see how to create threads, how to coordinate and synchronize them, and how to handle common problems that arise in threading.

intermediate best-practices

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated Aug. 13, 2026