Posts

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...

Hibernate-Object state transitions

Image
 Transient instances –instances of a persistent class instantiated with the new operator –transient, they have no persistent state –garbage collected if dereferenced by the application –have no database identity  Transient instances may be made persistent by –calling Session.save(object) –creating a reference from another instance that is already persistent  Persistent instances –include any instance retrieved with a query, lookup by identifier or navigation –are managed, changes are automatically flushed to the database –are transactional, changes can be rolled back in the database only –have database identity  Persistent instances may be made transient by –calling Session.delete(object) –“orphan delete” (later)  Detached instances –are instances with database identity that are not associated with any open Session –are no longer managed by Hibernate –represent database state, that is potentially stale  Persistent instances become detached by –calling Sessi...

Facts about HashMap in Java 6

Image
  Few points regarding HashMap DS in Java 6 as per the source code. HashMap extends AbstractMap implements Map Backed by array of Map.Entry where each element points to a list of Map.Entry  Default Initial Capacity – 16 i.e. Entry[16] Default load factor  - 0.75 i.e. when size reaches 75% of capacity – resizing of internal data structure is triggered. Capacity is always in power of 2 - if 5 is passed as initialCapacity in constructor, capacity will be set to 8 – it just does bitwise shift of 1 until it gets a number just greater than the passed capacity value. If 8 is passed – actual capacity will be 16 . (for detail check source code) provides a hook method init() which subclasses can use for addition functionality- it is invoked from constructor. modCount member variable to track the modifications in hashmap. same is used for Fail-fast detection modifications means adding new Entry and removing Entry  from hashmap provide supplemen...

ConcurrentHashMap

Image
J ava thread safety problems can bring down your Java EE application and the Java EE container fairly easily. One of most common problems I have observed when troubleshooting Java EE performance problems is infinite looping triggered from the non-thread safe HashMap get() and put() operations. There seem to be three different synchronized Map implementations in the Java API: Hashtable Collections.synchronizedMap(Map) ConcurrentHashMap ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java 1.5 as part of Java concurrency package. Prior to Java 1.5 if you need a Map implementation, which can be safely used in a concurrent and multi-threaded Java program, than, you only have Hashtable or synchronized Map because HashMap is not thread-safe. With ConcurrentHashMap, now you have better choice; because, not only it can be safely used in concurrent multi-threaded environment but also provides better performance...

Singleton Collection Classes

Image
Singletons are useful to provide a unique source of data or functionality to other Java Objects. Collection framework provides singleton classes for each collection class like SingletonList and SingletonMap.   In this article we will discuss about SingletonList which is equally applicable to other singleton collection classes. Collections Class provide method to convert normal list into singletonList like below syntax, List<String> mySingletonList = Collections. singletonList ( "AAA" ); Java documentation says "Returns an immutable list containing only the specified object. The returned list is serializable." Why we need this type single object Immutable list? Below is the code to remove all null values from list - in conventional style: package singleton; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SingletonListTest { public static void main (String[] args) { List ...

Thread Safety in Java

Image
Thread safety simply means that the fields of an object or class always maintain a valid state, as observed by other objects and classes, even when used concurrently by multiple threads. There are two big reasons you need to think about thread safety when you design classes and objects in Java: Support for multiple threads is built into the Java language and API All threads inside a Java virtual machine (JVM) share the same heap and method area Let see some example: // Instances of this class are NOT thread-safe. public class RGBColor { private int r; private int g; private int b; public RGBColor ( int r, int g, int b) { checkRGBVals(r, g, b); this . r = r; this . g = g; this . b = b; } public void setColor ( int r, int g, int b) { checkRGBVals(r, g, b); this . r = r; this . g = g; this . b = b; } // returns color in an array of...

Java Thread Communication

Image
Threads can be used to run more than one task at a time, you can keep one task from interfering with another task’s resources by using a lock (mutex) to synchronize the behavior of the two tasks. That is, if two tasks are stepping on each other over a shared resource (usually memory), you use a mutex to allow only one task at a time to access that resource. In this article we will make tasks cooperate with each other, so that multiple tasks can work together to solve a problem. Now the issue is not about interfering with one another, but rather about working in unison, since portions of such problems must be solved before other portions can be solved.The key issue when tasks are cooperating is handshaking between those tasks. We can use any of below methods for thread communication. (1) Easiest, reliable, but inefficient way is to let thread-A periodically wake up and test a condition flag that thread-B will update. Sample code as below, public class MyRunnable implements Runn...

Map sorting based on Values

Image
Map sorting based on Values (instead of keys) These days one of the very popular interview question is regarding having Map type collection with sorting based on values (Map is key->value pair). TreeMap can give you Key based sorting. Use case would be We have Student object (with student name and roll number properties) and we have Result object (having int result property). We have to use Student as key and Result as value in our Map but requirement is Map should be having sorting order based on values(result). I have tried to solve this using below program, Please suggest if there is better way or this can be improved. Result.java package collection; public class Result implements Comparable{ private int marks; public int getMarks () { return marks; } public void setMarks ( int marks) { this . marks = marks; } @Override public int hashCode () { final int prime = 31 ; int result = 1 ; result = prime * result + marks;...