Posts

Showing posts from 2018

Java8 CompletableFuture API for Asynchronous Programming in springboot microservices

Introduction Lambda expressions are by far the most discussed and emphasized feature of Java 8. While I agree that Lambdas being the salient feature in Java8, still there are lot more new things hidden behind the shadows of lambda expressions. Today we gonna take a look at one of them, the CompletionStage API [1] for leveraging asynchronous programming.

Java5 introduced the ExecutorService pattern where programmers can submit tasks to a pool of Threads. This yields a Future object and the only way to get the result from a future object is to call its get() method in the thread that submitted the task. This method is blocking, so this call will block the thread until the result is available to process. So, the use of  Future is ruled out on performance grounds. This is exactly where the CompletionStage comes to the rescue. [2]

What Is a CompletionStage In a nutshell, a CompletionStage is a model that carries a task. A task can be an instance of Runnable, Consumer, or Function. The ta…

Leveraging Multicore CPU architecture with fork-join framework in Java

Image
Introduction Well, today we are going to leverage Multicore CPU architecture using fork-join framework in Java. Fork-join framework was introduced in Java 7 and has not been covered in most of the Multithreading books released earlier. Today we have multicore CPUs and can implement parallel algorithms to leverage them. Introduction of Multicore CPUs has led to significant changes in more concurrency concepts and frameworks including Java. As we moved into the multicore era, the primary application of concurrency was to factor the workload into independent, coarse-grained tasks — with the aim of increasing throughput by processing multiple requests simultaneously. We are going to discuss how to execute Merge Sort algorithm in parallel by leveraging multi core CPU architecture today. At the end I am going to conduct a benchmark of the new parallel approach with the imperative approach which is suitable for mono core CPUs.

Prerequisites: Good knowledge of …