forEach Expression

Starting Java 8, a new method forEach is introduced to iterate the elements. This forEach(Consumer<? super T> action) method is created in the following interfaces in java. java.lang.Iterable java.util.Stream This method performs a user given action on each element. In the below example, we can iterate a list of collections with forEach defining a condition.  

Result:

Lambda Expressions

Lambda expression is a Java 8 feature, which helps the developer to simplify syntax of anonymous interface implementation whenever required. Interface is defined anonymously in earlier Java Implementation in the below old syntax., which is very much reduced in Java 8 lambda implementation, comparison of the below example provide a clear view of Lambda expression. … Continue reading “Lambda Expressions”

ForkJoinPool

ForkJoinPool is the core of the concurrency frameworks which follows divide and conquer approach in completing the tasks. Fork – Recursively break the single task into multiple smaller tasks till the threshold. Join – Join the multiple smaller tasks results and produce the output recursively. Initialise the maximum number of worker threads.

RecursiveAction class … Continue reading “ForkJoinPool”

Reentrant Lock

The ReentrantLock class is an implementation of Lock Interface, which will lock() the execution block for the thread which calls it.Until the locked thread calls the lock.unlock() method, the code block in between will not be available to other Threads…even when it is idle or sleeping or taking long time.Hence the block after lock.lock() is … Continue reading “Reentrant Lock”

Cyclic Barrier

Assume a the conference call will be opened when 4 users join., Assume until all the users join, that conference call wont be opened even when the less than 4 users join. Below example is a simple illustration of the scenario using Cyclic Barrier.

 Result:

Executor & Executor Service

Executor and ExecutorService are the utility classes present starting from JDK 1.5 for executing a java process/block parallely in a controlled manner. Executor – Executor is used for creates a thread pool to for fixed number, using the method newFixedThreadPool. e.g., We have a 2 dimensional array where each row needs to be processed or set to … Continue reading “Executor & Executor Service”

Semaphore

A semaphore maintains a fixed number of threads in executions if exceeds lock can be acquired for that semaphore thread. Below method will get the lock of semaphore within semaphore.availablePermits()

If there are more that fixed threads, it will wait to acquire/get the semaphore lock until it has semaphore.availablepermits() greater than ‘0’.A semaphore initialised to … Continue reading “Semaphore”

Binary To Decimal Conversions

This example shows the conversion of Decimal to Binary & Binary to Decimal  in Java. The toBinary() method in BinaryConversion class uses the recursion logic of modulo(%) 2 of given number(which is reminder), and calls the same method recursively with number/2( from 512>>2 =256) , until the reminder is one. The toDecimal() method uses the … Continue reading “Binary To Decimal Conversions”

Factory Design Pattern

Factory Pattern is a design pattern to create objects based on the type with the help of Factory class. Factory Class create and return the object based on the type of request, and Caller class not aware of which class returned, however the created class do the operation based on the requested type. In this … Continue reading “Factory Design Pattern”

Singleton Design Pattern

A Singleton design Pattern is a very simple design pattern where only one instance(object) is created for a Class. And the Object can be accessed publicly, however only one instance will be there available through out its life time. In Java, Singleton Pattern can be easily implemented by making the constructor to private and provide … Continue reading “Singleton Design Pattern”