What are Java Threads?
In Java, a thread is a "sub-process" or "lightweight process" that runs in the background. It allows your program to perform multiple tasks at the same time (Concurrent Execution). Every Java program has at least one thread: the Main Thread.
java
Comparisons: Two ways to Create Threads
Wait! Should you extend the
Thread class or use the Runnable interface?| Method | Behavior | Best Use Case... |
|---|---|---|
| Thread | Extend java.lang.Thread. | Quick, simple tasks. |
| Runnable | Implement java.lang.Runnable. | Recommended! (Better for long-term design). |
java
Managing Thread Execution
start(): Kicks off the background thread (Does NOT callrun()directly!).sleep(ms): Pauses the current thread for a specific amount of time.join(): Pauses the calling thread (usually Main) until the background thread finishes its work.
java
Why use Threads?
- Responsiveness: Keep your UI responsive while a background task (like a file download) is running.
- Performance: Use all the cores in your modern multi-core CPU.
- Scalability: Handle many simultaneous requests (like in a web server).
Summary
| Term | Description |
|---|---|
| Thread | A single execution "path" |
| Main | The first thread that runs the main() method |
| Runnable | The task that should be run by the thread |
| start() | Initiate the thread's execution |
| join() | Wait for a thread to die |
| sleep() | Temporarily pause a thread |
| Best For | Multi-tasking, background processing, CPU intensive apps |
| Safety | Threads must be carefully managed (See Synchronization!) |
| Modern | Use "Virtual Threads" (Java 21+) for massive scalability |
| Lambda | Threads are perfect with x -> ... logic! |
| Priority | t.setPriority(10) (Ask the OS to run it faster!) |
| Daemon | t.setDaemon(true) (A thread that won't stop the app from exiting) |
| Interrupted | Check if thread was told to stop with .isInterrupted() |
| Exception | InterruptedException must be caught during sleep/join |
| Lifecycle | NEW -> RUNNABLE -> BLOCKED -> WAITING -> TERMINATED |
| Native | Threads are "Mapped" to actual Operating System threads |
| Platform | Behavior might slightly differ between OSs |
| Standard | Established and trusted worldwide for 25+ years |
| Resource | Each thread uses memory for its own stack (usually ~1MB) |
| Context | Switching between 1000s of threads can slow down the CPU |
| Worker | A generic "Worker" thread that handles background data |
| UI Thread | Never perform long-running tasks in the main/UI thread! |
| Safety | Global variables are SHARED between all threads! |
| Deadlock | When two threads wait for each other FOREVER (AVOID!) |
| Scheduler | The JVM part that decides which thread runs next |