thread
A path of execution inside a process that the operating system schedules on a processor; one process can have many.
In more detail
Threads of the same process share its memory, which makes communication fast and data races possible. Creating many threads is expensive, so runtimes often multiplex many lightweight tasks onto a few threads.
Examples
In the browser: A browser tab uses a main thread for JavaScript, styles and layout, plus other threads for compositing, parsing and networking. Web Workers let your own code run on an extra thread so heavy work does not freeze the page.Go: the Go runtime runs many goroutines on a few threadsJava: new Thread(() -> System.out.println("work")).start(); calling run() instead of start() runs on the current thread.Thread worker = new Thread(() -> System.out.println("work")); worker.start();The kernel schedules threads, not whole processes, so a busy process with many threads can use several processors at once. Threads in one process share memory and open files, which makes communication cheap but mistakes contagious.Node.js runs your JavaScript on one main thread and uses extra threads for libuv's thread pool, garbage collection and worker threads.Related terms
- concurrency — Having several pieces of work in progress during overlapping periods of time, such as requests being handled at once.
- mutex — A lock that lets only one piece of code at a time run a critical section or do a particular piece of work.
- process — A program that is currently running, with its own memory and a numeric identifier.
- thread pool — A fixed or bounded set of reusable worker threads that take tasks from a queue, instead of creating a new thread for each task.
- async and await — Language keywords for writing code that waits for slow work, such as a network call, without blocking a thread.
- call stack — The region of memory where a running program keeps one frame for each function call that has started but not yet returned.
- main thread — The first thread a program starts with; in a browser page it also runs JavaScript and handles input, layout and painting.
- Web Worker — A script that runs on its own background thread, separate from the page's main thread, so heavy computation does not freeze the page.
- goroutine — A function running concurrently with the rest of a Go program, started by putting the go keyword before a call.
- Go scheduler — The part of the Go runtime that decides which goroutine runs on which operating system thread, and when.
- Runnable — The functional interface for a task that takes no input and returns no result, used to give a thread work to do.
- Thread.join — The method that makes the current thread wait until another thread has finished.
- ExecutorService — The standard-library service that owns a pool of worker threads and runs tasks you submit to it, returning a Future for each.
- race condition — A bug whose outcome depends on the timing of two things happening at once.
- CPU scheduler — The part of the kernel that decides which runnable thread gets a processor next and for how long.
- worker threads — The Node.js module for running JavaScript in parallel threads inside one process, each with its own V8 isolate and event loop.
Taught in
- Build, Release, Run And Processes — DevOps & Site Reliability Engineering
- Port Binding And Concurrency — DevOps & Site Reliability Engineering
- Async and Await: Managing Concurrency — Modern .NET Core, C# & Enterprise Microservices
- The Dependency Injection Container: Scopes and Lifetimes — Modern .NET Core, C# & Enterprise Microservices
- Querying, Change Tracking, and Saving Data — Modern .NET Core, C# & Enterprise Microservices
- HTML Parsing, Tokenization And The Document Object Model (DOM) — Frontend Architecture & Web Platform
- Passive Listeners, Scroll Performance And Touch Interactions — Frontend Architecture & Web Platform
- Web Workers: Off-Thread Computation And Structured Clone — Frontend Architecture & Web Platform
- Goroutines and Concurrent Execution — Concurrent Systems Programming with Go
- Advanced Stream Operations — Modern Java: Virtual Threads & High-Throughput Services
- Concurrency Basics: Threads And Runnable — Modern Java: Virtual Threads & High-Throughput Services
- The ExecutorService — Modern Java: Virtual Threads & High-Throughput Services
- Objects, Nested Classes And Garbage Collection — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Checked, Unchecked, Catch And Finally — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Lambdas And Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Jars, Automatic Modules And Runtime Images — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Platform And Virtual Threads, Runnable And Callable — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Thread Safety: Locks And Atomics — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Concurrent Collections And Parallel Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Modules And Concurrency — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Mock Exam: Sit It, Score It — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Final Review And Exam Day — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- What A Process Is — Linux Fundamentals & Systems Administration
- libuv Architecture: Event Demultiplexer & Thread Pool — Node.js Internals & Backend Services
- Event Loop Lifecycle: The Six Phases & libuv uv_run — Node.js Internals & Backend Services
- Worker Threads: worker_threads & SharedArrayBuffer — Node.js Internals & Backend Services
- Thread Pooling: Piscina & CPU-Bound Workload Pools — Node.js Internals & Backend Services
- Cache Invalidation And The Three Hard Cases — Scalable System Design & Distributed Architecture
- Observability As A Design Input — Scalable System Design & Distributed Architecture
- Capacity Planning As A Design Input — Scalable System Design & Distributed Architecture
- Worked Design Two: A Global Chat System — Scalable System Design & Distributed Architecture
- Worked Design Two: Scaling Chat Fan Out — Scalable System Design & Distributed Architecture
References
- pthreads(7), Linux manual page
- The Twelve-Factor App: VIII. Concurrency
- Threads and threading
- MDN glossary: Thread
- Java SE 21 API: Thread
- Java Tutorials: Processes and Threads
- Java Tutorials: Defining and Starting a Thread
- man7: clone(2)