Junit 4 Test Suite Example in Java

In this example I am going to explain how to use a test suite in Junit testing framework in Java programming language. A test suite bundles a few unit test cases and runs them together. In Junit, both @RunWith and @Suite annotations are used to run the suite test. Junit Classes : Junit classes are important classes which used in…

CDI (Context Dependency Injection)

Introduction I will discuss here about the Context Dependency Injection (CDI). First of all I will tell you what is Context Dependency Injection as follows. Context Dependency Injection (CDI): knit the web and transactional tiers together of the JEE platform is a set of services used together allows developers to use enterprise beans along with Java Server Faces (JSF) technology…

CompletableFuture in Java 8 or later

Introduction Here I will discuss about Java 8 or later version’s new feature CompletableFuture in Java programming language. Using Java 8 or later version’s CompletableFuture API you can complete the tasks in an ad hoc manner. A Future represents the pending result of an asynchronous computation. It offers a method — get() — that returns the result of the computation…

ReadWriteLock in Java

A ReadWriteLock interface in Java is more sophisticated than the Lock interface. Imagine you have an application that reads from and writes to some resources, but reading frequency is much more higher than writing frequency. Two or more threads reading the same resource do not cause any problem for each other, i.e., multiple threads reading the same resource are granted…

ReentrantLock in Java

A Reentrant lock is an implementation of java.util.concurrent.Lock interface with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities. Lock is acquired by lock() method and held by Thread until a call to unlock() method. ReentrantLock provides same visibility and ordering guarantee, provided by implicit locking mechanism(synchronized), which…

Lock in Java

Introduction You may be already aware of basic concepts around thread synchronization and various mechanisms using synchronized keyword. A lock interface is a thread synchronization aid like synchronized block except lock can be used in a more sophisticated way than Java’s synchronized blocks. A lock is a tool for controlling access to a shared resource by multiple threads. Commonly, a…

Copy Constructor in Java

What is Copy Constructor? Sometimes a programmer wants to create an exact but separate copy of an existing object so that subsequent changes to the copy should not alter the original or vice versa. This is made possible using the copy constructor. A copy constructor is a constructor that creates a new object using an existing object of the same…

How To Add Unique Objects In Java HashSet

Introduction Here in this example I am going to show you how to add unique objects in Java HashSet. Set interface in Java maintains uniqueness, so any duplicacy is not allowed in Set interface. The HashSet class maintains uniqueness through HashMap, so HashSet uses internally HashMap to determine unique elements. When you add String type data or any primitive type…

CopyOnWriteArrayList in Java

What is CopyOnWriteArrayList CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrent API. It implements List interface like ArrayList, Vector and LinkedList but it is a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. CopyOnWriteArrayList creates a fresh copy of the underlying array to implement all mutative…

Junit Code Coverage with Jacoco

Introduction In this post I will show you how to work with Junit code coverage with JaCoCo code coverage library, which is quite a new maven plug-in that provides the JaCoCo runtime agent to your tests and allows basic report creation. Currently it supports instruction, branch, line, method and class coverage which is pretty enough you can expect from this…