1. Thread Inheritance Technique

Technical Explanation

  • Mechanism: Extends java.lang.Thread class
  • Core Method: Override run() method
  • Thread Lifecycle Management: Directly controlled by subclass
  • Inheritance Limitation: Java supports single inheritance

// 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
                                        

2. Runnable Interface Technique

Technical Explanation

  • Mechanism: Implements java.lang.Runnable interface
  • Core Method: Implement run() method
  • Thread Creation: Pass to Thread constructor
  • Flexibility: Allows multiple interface implementation

// 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
                                        

3. Lambda Thread Creation

Technical Explanation

  • Mechanism: Functional interface implementation
  • Core Concept: Compact, inline thread definition
  • Java Version: Introduced in Java 8+
  • Syntax: () -> { /* Thread Logic */ }

// 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");
});