Posts

Showing posts with the label java

Lambda Expression v/s Anonymous Inner Class - Java 8

Image
Lambda Expression is new feature of Java 8. Before Java 8 Anonymous Inner Class was very useful to provide method local implementation of any interface like ActionListner or EventListener etc. With Java 8 if the interface is a Functional Interface we can use Lambda Expression to provide implementation. Both of these can implement an Interface but there are subtle differences between them. Following are to the point differences between them - Anonymous Inner class can implement any interface but Lambda can implement only Functional Interface , the interface with only one abstract method. Anonymous Inner Class can have state variables but Lambda Expression cannot. Lambda Expression is stateless. Lambda Expression can only access final or effectively final variables of containing method. Scope - Anonymous Inner Class creates a new scope whereas Lambda Expression does not. Lambda executes always within scope of surrounding context. To explain it better, in Anonymous Inner Clas...

Lambda Expression Basics and Syntax - Java8

Image
In simple term, a Lambda Expression is implementation of a Functional Interface. Lambda Expression is a way to provide implementation of a functional interface without creating a Class or Anonymous Inner Class. Note that Lambda can implement only a functional interface. Read more about Functional Interface Here is a functional interface named Processor which has single abstract method called process And let say there is processValue method, which takes a value and a Processor object as input, apply processor on the value and return the result. private int processValue(int value, Processor processor) { return processor.process(value); } Call processValue method using Anonymous Inner Inner Class as implementation of Processor interface. processValue(10, new Processor() { @Override public int process(int input) { return input * 2; } }); It’s easily noticeable the unnecessary verbose code that we had to write for anonymous inner class. Only important line which is d...

Java Thread Local Storage explained in easiest practical way

Image
One of the cool feature of Java that I like is ThreadLocal storage. ThreadLocal storage is explained in easiest way with practical example in this blog. ThreadLocal is a class in java.lang package which provides a way to store and access data of any type per thread, which means each thread will have it's separate instance of the data. During execution of a thread, one class can set data in ThreadLocal and other classes further down in the execution layer can access/read the data, as long as execution in within same thread. It is important to know that ThreadLocal data set by one thread is not visible or accessible in another thread. That the core concept of ThreadLocal storage. The way to use ThreadLocal is declare a static property of type ThreadLocal where T is type of the data we want to store. It's static so that it easily accessible in classes wherever needed. You can declare it public, private or whatever make sense in your usecase. Usually declaring it as...

EhCache3 as JCache (JSR-107) Implementation with Cache Statistics

Image
If you are reading this you already know that caching is mechanism to store static data In-Memory for faster access and avoid expensive database calls or any kind of service calls to get the data. Read my other post to learn InMemory Caching using EhCache3 . There are many ways to implement cache in java based applications, starting from very simple Map based caching to Apache Common JCS , EhCache , Apache Ignite , Hazelcast , Oracle Coherence and many more. With so many implementations available, question is which one we should go for?  If you have to choose one from multiple options best bet is to look for standard. Standard because many people have put their mind together in it, better support, examples and guide available for reference plus expect that further upgrades/changes will not breaking changes. So for InMemory cache Standard Specification is JSR-107 .  You can read about it in more detail at JSR 107: JCACHE - Java Temporary Caching API . JCache speci...

EhCache3 In Memory Caching for Performance Improvement

Image
If you are reading this you already know that caching is mechanism to store static data In-Memory for faster access and avoid expensive database calls or any kind of service calls to get the data. There are many ways to implement cache in java based applications, starting from very simple Map based caching to Apache Common JCS , EhCache , Apache Ignite , Hazelcast , Oracle Coherence and many more. Each of them have pros and cons. I will show how to use EhCache for caching implementation EhCache3 Implementation For this example you need to have following jars in the class path - <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.4.0</version> </dependency> <dependency> <!-- We need this because ehcache uses slf4j for logging --> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version>...