Java Threads Introduction
In this page:
What is a Thread?
A thread is a lightweight sub-process that executes concurrently inside your application, sharing the same memory space as every other thread in that process. This shared memory makes threads far cheaper to create than separate processes, enabling fast, non-blocking background work like network calls or file I/O.
Example: What is a Thread?
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> System.out.println("Running in background thread"));
t.start();
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
The Thread Class
You can declare a custom thread by extending the built-in Thread class and overriding its run() method with the code that should execute in the background. This approach is straightforward but ties up your only chance at inheritance, since Java classes can only extend one parent.
Example: The Thread Class
public class Main {
static class MyThread extends Thread {
public void run() {
System.out.println("Custom thread running");
}
}
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Starting a Thread
To actually start a thread, you initialize your Thread object and call its start() method rather than calling run() directly. Calling start() registers the thread with the JVM's scheduler, which then decides when to actually execute the run() method on a separate call stack.
Example: Starting a Thread
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> System.out.println("Executed via start()"));
t.start(); // registers with JVM scheduler
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Main Thread Overview
Every Java program begins execution with a single main thread automatically created by the JVM before your main() method even runs. You can inspect and interact with this thread at any point using Thread.currentThread(), which is handy for logging or debugging concurrency issues.
Example: Main Thread Overview
public class Main {
public static void main(String[] args) {
Thread current = Thread.currentThread();
System.out.println("Running on: " + current.getName());
}
}
Login to try C/C++/Java/PHP code in the editor
Multi-threaded Execution
By spawning multiple threads, a program can execute distinct tasks concurrently and take advantage of multiple CPU cores instead of running everything sequentially on one. This is especially valuable for CPU-bound work that can be split into independent chunks, or I/O-bound work that would otherwise leave a core idle while waiting.
Example: Multi-threaded Execution
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> System.out.println("Task 1"));
Thread t2 = new Thread(() -> System.out.println("Task 2"));
t1.start();
t2.start();
t1.join();
t2.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: