Posts

Showing posts with the label Multithreading

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

Semaphores - Java Concurrency

Image
Semaphores are an often misunderstood and under used tool for restricting access to resources. They are ignored for other ways of controlling access to resources. But semaphores give us a tool set that goes beyond what normal synchronization and other tools can give us. The simplest way to describe a semaphore is a mechanism to allows n units to be acquired to access particular resource. A semaphore is a synchronization object that controls access by multiple processes to a common resource in a parallel programming environment. Semaphores are widely used to control access to files and shared memory. Semaphore is a technique used to control access to common resource (like database connection,  file operation,  ports etc) for competing multiple processes. Semaphore maintains a counter which keeps track of the number of resources available. When a process requests access to resource, semaphore checks the variable count and if it is less than total count then grants access an...

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

Atomic Variables in Java

Atomic is a toolkit of variable java.util.concurrent.atomic package classes, which assist in writing lock and wait-free algorithms with the Java language. When a data (typically a variable) can be accessed by several threads, you must synchronize the access to the data to ensure visibility and correctness. By example, if you have a simple counter: public class Counter { private int value; public int getValue(){ return value; } public int getNextValue(){ return value++; } public int getPreviousValue(){ return value--; } } This class works really well in single-threaded environment, but don’t work at all when several threads access the same Counter instance. You can solve the problem using synchronized method as below public class SynchronizedCounter { private int value; public synchronized int getValue(){ return value; } public synchronized int getNextValue(){ return value++; } public synchronize...

Race Condition in Java

Race condition in Java  is a type of concurrency bug or issue which is introduced in your program because  parallel execution of your program by multiple threads at same time, Since Java is a multi-threaded programming language hence risk of Race condition is higher in Java which demands clear understanding of what causes a race condition and how to avoid that. Anyway Race conditions are just one of hazards or riskpresented by  use of multi-threading in Java just like deadlock in Java.  Race conditions  occurs when two thread operate on same object without proper synchronization and there operation interleaves on each other. Classical  example  of Race condition  is incrementing a counter since increment is not an atomic operation and can be further divided into three steps like read, update and write. if two threads tries to increment count at same time and if they read same value because of interleaving of read operation of one thread to update operation of another thread, one count ...