Lambda Expressions | Concurrent Programming Approach 4 Last Updated : 08 Apr, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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() { @Override 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: Java 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++) { System.out.println( Thread .currentThread() .getName() + " Counter:" + j); pause(Math.random()); } }); } taskList.shutdown(); } // Pauses execution allowing time for // system to switch back and forth private void pause(double seconds) { try { Thread.sleep( Math.round(1000.0 * seconds)); } catch (InterruptedException e) { e.printStackTrace(); } } } Output: 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. Comment More infoAdvertise with us Next Article Lambda Expressions | Concurrent Programming Approach 4 A AnureetKaur Follow Improve Article Tags : Java Java Programs Java-Multithreading Practice Tags : Java Similar Reads Different Approaches to Concurrent Programming in Java This article shows how to perform concurrent programming using Java threading framework. Let's analyze concurrent programming first: Concurrent Programming: This means that tasks appear to run simultaneously, but under the hood, the system might really be switching back and forth between the tasks. 7 min read Main App Implements Runnable | Concurrent Programming Approach 2 Prerequisite: Different Approaches to Concurrent Programming in Java Let's look at the second approach in detail. The user has the main class that implements runnable which is a promise to the compiler that the class will have a run method. public class MyClass implements Runnable{ public void run() 3 min read Java Program to Implement ConcurrentLinkedQueue API The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts e 4 min read Java Program to Implement ConcurrentLinkedDeque API ConcurrentLinkedDeque class in Java is an unbounded concurrent deque that stores its elements as linked nodes where each node contains the address of the previous as well as next nodes. It belongs to java.util.concurrent package. This class is a member of the Java Collections Framework. It also exte 3 min read Java Program to Implement ConcurrentSkipListMap API ConcurrentSkipListMap API is based on the implementation of the ConcurrentNavigableMap. This map is sorted according to the natural ordering of its keys or by a Comparator provided on the map during creation time. This class implements a concurrent variant of SkipLists which makes its expected avera 10 min read Like