Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Thread

Поток

A running program can have more than one sequence of instructions in progress. Each such sequence is a thread, and threads in the same process can share data in memory.

Also known as: тред

What a thread does

Imagine an editor saving a large file. If the same thread handles clicks and waits for the save to finish, the window may stop responding. The editor can instead hand the save to a worker thread and keep handling input on the UI thread. The save doesn't have to get any faster for the program to feel more responsive.

What threads share

Threads in a process share its address space, so two threads can read or modify the same object if both can reach it. Each thread has its own call stack and execution state, including a record of where to resume. A local variable can still refer to a shared object: a separate stack does not make everything reachable from that stack private.

The operating system schedules native threads. A thread waiting for a blocking read may stop running until data is available, letting another thread use the CPU. On one logical core, threads take turns; on multiple logical cores, they can run at the same time. The first case is concurrent, while the second can also be parallel.

Example: waiting for a worker

from threading import Thread

def save_report():
    with open('report.txt', 'w', encoding='utf-8') as report:
        report.write('Total: 42')

worker = Thread(target=save_report)
worker.start()
# The main thread can do independent work here.
worker.join()

start() lets the worker run on a separate thread. join() waits for that thread to finish; it doesn't return the function's result or tell you whether the write succeeded. Putting join() immediately after start() leaves no opportunity for the calling thread to do useful work while it waits.

When to use threads

Threads are useful for keeping a UI responsive, overlapping blocking I/O, or splitting CPU work when the runtime supports parallel execution for that workload. A thread pool reuses workers instead of creating a new thread for every job. Adding more workers can hurt performance if they spend their time switching or contending for the same lock. An async task isn't necessarily a thread: one event-loop thread can make progress on several tasks as their I/O becomes ready.

In interviews

A common exercise is to explain how two threads can both increment a counter but produce only one increase. If incrementing means reading, adding, and writing, both threads can read the same old value before either writes. A mutex must cover the whole update, not just the write. An atomic increment is another option for a single counter.