// Thread Inheritance Technique
class MyThread extends Thread {
public void run() {
// Thread's core logic
System.out.println("Thread running: " + getName());
}
}
// Usage
MyThread thread = new MyThread();
thread.start(); // Starts thread execution
// Runnable Interface Technique
class MyRunnable implements Runnable {
public void run() {
// Thread's core logic
System.out.println("Runnable thread executing");
}
}
// Usage
Thread thread = new Thread(new MyRunnable());
thread.start(); // Starts thread execution
// Lambda Thread Creation (Java 8+)
Thread thread = new Thread(() -> {
// Inline thread logic
System.out.println("Lambda thread executing");
});
thread.start(); // Starts thread execution
// With ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(4);
executor.submit(() -> {
// Concise thread task
System.out.println("Executor lambda thread");
});