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

Java Threads Introduction

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

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

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
public class Main {
	public static void main(String[] args) {
		Thread current = Thread.currentThread();
		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
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 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.