Lambda Expressions | Concurrent Programming Approach 4

Prerequisite: Different Approaches to Concurrent Programming in Java

Lambda expressions are very similar in behaviour to anonymous inner classes. They have complete access to code from surrounding classes including the private data. They are much more concise, succinct and readable. However, lambda expressions cannot have instance variables, so they do not maintain a state.

We can replace Anonymous Inner Class with Lambda Expression as follows:

Anonymous Inner Class:

    Thread thread = new Thread(new Runnable() {
      public void run() {
        System.out.println("Printed inside Anonymous Inner Class!");

Lambda Expression:

Thread anotherThread = new Thread(() -> System.out.println(“Printed inside Lambda!”));

Practical Implementation:





import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// Code for Concurrent programming using
// Lambda Expression
public class MyClass {
    // Driver method
    public static void main(String[] args)
        new MyClass().startThreads();
    // Starting threads with pool size as 2
    private void startThreads()
        ExecutorService taskList
            = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 5; i++) {
            int finalI = i + 1;
            // Prints thread name and value
            // of the counter variable
            taskList.execute(() -> {
                for (int j = 0; j < finalI; j++) {
                        + " Counter:" + j);
    // Pauses execution allowing time for
    // system to switch back and forth
    private void pause(double seconds)
        try {
                Math.round(1000.0 * seconds));
        catch (InterruptedException e) {



pool-1-thread-1 Counter:0
pool-1-thread-2 Counter:0
pool-1-thread-2 Counter:1
pool-1-thread-2 Counter:0
pool-1-thread-1 Counter:0
pool-1-thread-2 Counter:1
pool-1-thread-1 Counter:1
pool-1-thread-2 Counter:2
pool-1-thread-1 Counter:2
pool-1-thread-2 Counter:0
pool-1-thread-1 Counter:3
pool-1-thread-2 Counter:1
pool-1-thread-2 Counter:2
pool-1-thread-2 Counter:3
pool-1-thread-2 Counter:4

Advantages: The user can easily access data in the main class including the private data. Lambdas are concise, succinct and readable.

Disadvantages: Lambdas are not allowed to have instance variables, thus we cannot pass arguments to run method. Also, we typically use lambdas when we have some shared data which invites the danger of race conditions.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.