Posts

Showing posts from June, 2013

Observability Done Right: Best Practices and Anti-Patterns for Effective System Monitoring

Image
  WHAT Observability is a concept that refers to the ability to gain insights into the behavior and performance of complex systems. In the context of software engineering, observability involves the collection, analysis, and visualization of data from software applications, infrastructure, and other components of a system. In the animal kingdom, observability plays a critical role in survival, allowing animals to monitor their surroundings, detect threats, and find food. Dolphins use echolocation to observe their surroundings. They emit high-frequency sounds that bounce off objects, allowing them to create a 3D map of their environment. Thanks for reading Knowledge Cafe! Subscribe for free to receive new posts and support my work. Subscribed WHY In today's era, architectures are becoming increasingly large, complex, and fast-paced due to the faster development and deployment of software by distributed teams with the help of DevOps, continuous delivery, and agile development methodo...

Serial Version ID in Java

Image
This blog is continuation of earlier post on Java serialization. Defination Serial Version UID is used to ensure that during deserialization the same class (that was used during serialize process) is loaded. The  serialVersionUID  is a universal version identifier for a  Serializable  class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an  InvalidClassException  is thrown. Serial version UID Syntax [ANY-ACCESS-MODIFIER] static final long serialVersionUID = 123L;  serialVersionUID is a static final field. You can assign any number of your choice to it. Later I will explain the significance of these two statements. Why serial versionUID? Lets start with annoying warning message you get in your IDE when you declare a class as Serializable. The serializable class Employee does  declare a static final serialVersionUID field of type long. Most of us used t...

IdentityHashMap in java

Image
IdentityHashMap class implements the  Map  interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an  IdentityHashMap , two keys  k1  and  k2  are considered equal if and only if  (k1==k2) . (In normal  Map  implementations (like  HashMap ) two keys  k1  and  k2  are considered equal if and only if  (k1==null ? k2==null : k1.equals(k2)) .) This class is  not  a general-purpose  Map  implementation! While this class implements the  Map  interface, it intentionally violates  Map's  general contract, which mandates the use of the  equals  method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required. Difference between  IdentityHashMap  and HashMap Though both HashMap and IdentityHashMap implements Map...

Immutable Collection in Java

Writing or creating immutable collections in Java is becoming popular day by day, because of concurrency and multithreading advantage provided by immutable objects. Immutable objects offers several benefits over conventional mutable object, especially while creating concurrent Java application. Immutable object not only guarantees safe publication of object’s state, but also can be shared among other threads without any external synchronization. Making instance variable final will not work as collection is storing reference of objects: final List<String> finallist = new ArrayList(){{add( "123" );add( "234" );add( "345" );}}; finallist. add ( "345" ); java.util.Collection and java.util.Arrays Classes provides way to create Immutable Collection which can be shared between threads or methods. Lets take an example to understand this concept. Below Java program has two parts (1)First we are creating array of String objects and convert...

Java Double Brace Initialization

Image
Double brace initialization is a combination of two separate process in java. There are two { braces involved in it. If you see two consecutive curly braces { in java code, it is an usage of double brace initialization. JavaLanguage doesn't have a convenient literal syntax for collections (lists, maps, sets, etc.). This makes creating constant collections or passing collections to functions quite laborious. Every time you have to Declare a variable for a temporary collection Create a new empty collection and store a reference to it in the variable Put things into the collection Pass the collection to the method Below Sample code explain conventional style. package doublebracecollection; import java.util.HashSet; import java.util.Set; public class NormalInitialization { public static void main (String[] args) { Set<String> params = new HashSet<String>(); params. add ( "one" ); params. add ( ...

Overriding Vs Hiding Methods

Image
Instance Methods An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method. The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type . When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, it will generate an error. If a subclass defines a class method with the same signature as a...

Difference between Stack vs Heap in Java

Image
What Is Stack? Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread. A Java virtual machine stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous. What Is Heap? The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage managem...

Java Threading – Executor Framework and Callable Interface

Concurrency is the ability to run several parts of a program or several programs in parallel. If time consuming tasks can be performed asynchronously or in parallel, this improve the throughput and the interactivity of your program. Executors framework (java.util.concurrent.Executor), released with the JDK 5 in package java.util.concurrent is used to run the Runnable objects without creating new threads every time and mostly re-using the already created threads. Thread pools manage a pool of worker threads. The thread pools contains a work queue which holds tasks waiting to get executed. A thread pool can be described as a collection of  Runnable  objects (work queue) and a connections of running threads. These threads are constantly running and are checking the work query for new work. If there is new work to be done they execute this Runnable. The Thread class itself provides a method, e.g. execute(Runnable r) to add a new  Runnable  object to the work queue. The Executor framework p...