← Back to Advanced Java Course | Chapter 2: Multithreading | Lesson 1 of 8

Java Threads Introduction

A thread is a separate line of work inside a program, like having two cooks in one kitchen at the same time. It lets a program do several things together.
Syntax
java
class ClassName extends Thread {
    public void run() {
        // thread code
    }
}

new ClassName().start();

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?

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws InterruptedException {
		// Create a new Thread object and store it in t
		Thread t = new Thread(() -> System.out.println("Running in background thread"));
		// Start the thread; it runs in parallel with this code
		t.start();
		// Wait for the thread to finish
		t.join();
	}
}

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

java
// Define the class Main
public class Main {
	// Define the class MyThread
	static class MyThread extends Thread {
		public void run() {
			// Print a line to the console
			System.out.println("Custom thread running");
		}
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws InterruptedException {
		// Create a new MyThread object and store it in t
		MyThread t = new MyThread();
		// Start the thread; it runs in parallel with this code
		t.start();
		// Wait for the thread to finish
		t.join();
	}
}

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

java
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();
	}
}

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

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Thread current = Thread.currentThread();
		// Print a line to the console
		System.out.println("Running on: " + current.getName());
	}
}

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

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws InterruptedException {
		// Create a new Thread object and store it in t1
		Thread t1 = new Thread(() -> System.out.println("Task 1"));
		// Create a new Thread object and store it in t2
		Thread t2 = new Thread(() -> System.out.println("Task 2"));
		// Start the thread; it runs in parallel with this code
		t1.start();
		// Start the thread; it runs in parallel with this code
		t2.start();
		// Wait for the thread to finish
		t1.join();
		// Wait for the thread to finish
		t2.join();
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling t.run() instead of t.start(), which just runs the code in the current thread instead of creating a new one.
  2. Calling start() twice on the same Thread, which throws an IllegalThreadStateException.
  3. Assuming threads print in a fixed order, when the scheduler decides, so output can differ between runs unless you use join() or synchronization.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.