Thread Implementation Approaches
Approach 1: Extending Thread Class
Create a custom thread class by extending the Thread class:
class CustomThread extends Thread {
@Override
public void executeTask() {
while (true) {
System.out.println("Thread is executing task...");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public class ThreadExample1 {
public static void main(String[] args) throws InterruptedException {
CustomThread worker = new CustomThread();
worker.start();
while (true) {
System.out.println("Main thread executing...");
Thread.sleep(1000);
}
}
}
Key Points:
- Both start() and run() are Thread class methods
- run() defines thread execution logic
- start() creates new OS thread that invokes run()
Approach 2: Implementing Runnable Interface
Implement Runnable interface to create thread logic:
class TaskExecutor implements Runnable {
@Override
public void executeTask() {
while (true) {
System.out.println("Thread performing operation...");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public class ThreadExample2 {
public static void main(String[] args) throws InterruptedException {
Thread workerThread = new Thread(new TaskExecutor());
workerThread.start();
while (true) {
System.out.println("Main thread working...");
Thread.sleep(1000);
}
}
}
The Thread constructor accepts Runnable implementation instances.
Approach 3: Anonymous Thread Subclass
Create thread using anonymous inner class extending Thread:
public class ThreadExample3 {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread() {
@Override
public void executeTask() {
while (true) {
System.out.println("Anonymous thread running...");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
};
worker.start();
while (true) {
System.out.println("Main process executing...");
Thread.sleep(1000);
}
}
}
Anonymous classes create unnamed Thread subclasses with overridden methods.
Approach 4: Anonymous Runnable Implementation
Use anonymous class implementing Runnbale interface:
public class ThreadExample4 {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(new Runnable() {
@Override
public void executeTask() {
while (true) {
System.out.println("Runnable thread active...");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
});
worker.start();
while (true) {
System.out.println("Primary thread operating...");
Thread.sleep(1000);
}
}
}
Approach 5: Lambda Expression Implementation
Recommended approach using lambda expressions:
public class ThreadExample5 {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
while (true) {
System.out.println("Lambda thread processing...");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
});
worker.start();
while (true) {
System.out.println("Main execution flow...");
Thread.sleep(1000);
}
}
}
Lambda expressions provide concise Runnable implementation without explicit method overriding.