Java Multithreading Implementations
This article explains three common ways to create multithreaded Java programs—extending Thread, implementing Runnable, and using Lambda expressions—providing step‑by‑step instructions, code samples, console output, and a summary of each method’s advantages and appropriate use cases.
Java provides several ways to create new threads. The most common are extending java.lang.Thread , implementing java.lang.Runnable , and using a Lambda expression that implements Runnable (available since Java 8).
1. Extending Thread
Define a subclass of Thread and override its run() method with the code that should execute in the new thread. Create an instance of the subclass and call start() to launch the thread.
package org.funtester.performance.books.chapter01.section2;
public class FunThreadFirst extends Thread {
public static void main(String[] args) {
new FunThreadFirst().start(); // start thread
System.out.println("Hello FunTester! " + Thread.currentThread().getName());
}
@Override
public void run() {
try {
Thread.sleep(100); // sleep 100 ms
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Hello FunTester! " + Thread.currentThread().getName());
}
}Console output shows that the main thread prints first, followed by the child thread, demonstrating concurrent, non‑blocking execution.
2. Implementing Runnable
Create a class that implements Runnable and provides a run() method. Instantiate the class, pass it to a new Thread object, and start the thread.
package org.funtester.performance.books.chapter01.section2;
public class FunThreadSecond implements Runnable {
public static void main(String[] args) {
FunThreadSecond second = new FunThreadSecond(); // create Runnable instance
new Thread(second).start(); // start thread
System.out.println("Hello FunTester! " + Thread.currentThread().getName());
}
@Override
public void run() {
try {
Thread.sleep(100); // sleep 100 ms
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Hello FunTester! " + Thread.currentThread().getName());
}
}The output order is the same as the previous example, confirming that the two threads run independently.
3. Using a Lambda Expression
Since Java 8, a Lambda can be used to provide a concise implementation of Runnable . The Lambda is passed directly to the Thread constructor.
package org.funtester.performance.books.chapter01.section2;
public class FunThreadThird {
public static void main(String[] args) {
new Thread(() -> System.out.println("Hello FunTester! " + Thread.currentThread().getName())).start(); // create and start thread
System.out.println("Hello FunTester! " + Thread.currentThread().getName());
}
}The console output again shows the main thread followed by the new thread, illustrating non‑blocking behavior.
Summary
Extending Thread : suitable when you need to add functionality to the thread class itself, but Java’s single‑inheritance limitation may restrict design.
Implementing Runnable : more flexible, allows multiple threads to share the same task logic.
Using Lambda expressions : concise syntax, recommended for Java 8+ projects.
Choose the approach that best fits the specific requirements of your application.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.