Posts

Showing posts from July, 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...

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