Implementing Threads in Java with Various Approaches Including Lambda Expressions

Jaava provides four primary appproaches for thread implementasion:

  1. Extending the Thread class
  2. Implementing the Runnable interface
  3. Using anonymous inner classes
  4. Leveraging Lambda expressions

Runnable Interface Implementation

public TaskRunner(String name) {
    this.taskName = name;
}

@Override
public void run() {
    for(int count=0; count<10; count++) {
        System.out.println(taskName + " executing, count: " + count);
    }
}

}

public class RunnableExample { public static void main(String[] args) { TaskRunner task1 = new TaskRunner("Worker 1"); TaskRunner task2 = new TaskRunner("Worker 2");

    Thread worker1 = new Thread(task1);
    Thread worker2 = new Thread(task2);
    
    worker1.start();
    worker2.start();
}

}


</div>### Thread Class Extension

<div>```
class CustomThread extends Thread {
    private String threadName;
    
    public CustomThread(String name) {
        this.threadName = name;
    }
    
    @Override
    public void run() {
        for(int iteration=0; iteration<10; iteration++) {
            System.out.println(threadName + " processing, iteration: " + iteration);
        }
    }
}

public class ThreadExtensionExample {
    public static void main(String[] args) {
        CustomThread thread1 = new CustomThread("Processor A");
        CustomThread thread2 = new CustomThread("Processor B");
        
        thread1.start();
        thread2.start();
    }
}

Standard format: (parameter list) -> {implementation code}

Key simplification rules for Lambda expressions:

  • Parameter types can be omitted when inferable
  • Parentheses can be omitted for single parameters
  • Braces, return, and semicolon can be omitted for single-line implementations

Tags: java multithreading Runnable thread lambda

Posted on Tue, 28 Jul 2026 16:05:30 +0000 by tmallen