Read first

A thread is one line of execution inside a program. A process is a running program with its own memory; it has at least one thread, and can have many. Threads in the same process share memory (the heap, global variables) but each has its own call stack.

How it works

  • The operating system scheduler switches between threads (a context switch) many times a second. On a machine with several CPU cores, threads can also run truly at the same moment.
  • Sharing memory makes threads cheap to communicate, and dangerous: two threads changing the same data can interleave in surprising ways (a race condition). Locks prevent that, and bring their own problem (deadlock).

In the languages you use

LanguageThreads
JavaScriptYour code runs on one main thread; the event loop gives it concurrency. Real extra threads come from Web Workers (browser) or worker_threads (Node), which don’t share normal variables.
Python (CPython)Real OS threads, but the GIL lets only one run Python bytecode at a time, so threads help with waiting (I/O), not with CPU-heavy work. Python 3.13 added an optional free-threaded build without the GIL (PEP 703).
Java, C#Real threads that run in parallel on multiple cores, plus thread pools and higher-level tools.

Example

import threading, time
 
def fetch(name):
    time.sleep(0.2)          # stands in for a network call; the GIL is released while waiting
    print("done", name)
 
start = time.time()
threads = [threading.Thread(target=fetch, args=(i,)) for i in range(3)]
for t in threads: t.start()
for t in threads: t.join()
print(round(time.time() - start, 1) < 0.5)   # True: the three waits overlapped

Check yourself

Leads to

  • #1 · The event loop: How JavaScript does many things at once with one thread, and why promise callbacks run before timers.
  • Concurrency vs parallelism: Concurrency is dealing with many things in progress at once; parallelism is doing many things at the same instant.
  • GIL (Global Interpreter Lock): A lock in CPython that lets only one thread run Python bytecode at a time. Python 3.13 added an optional build without it.
  • Web Worker: A real background thread for JavaScript in the browser. It has no DOM access and talks to the page by passing messages.