Posts

Showing posts with the label synchronized

Synchronized Queue

Till Java 5 (jdk1.5) developer has to take care of synchronizing queue in multi threaded application. But in Java5 new synchronized implementation java.util.concurrent.BlockingQueue has been introduced. It extends java.util.Queue and introduced few new methods like put, take etc. BlockingQueue supports additional operations that waits for the queue to become non-empty when retrieving an element from queue, and wait for space to become available in the queue when storing an element. There are following few implementation available ArrayBlockingQueue : A simple bounded BloickingQueue implementation backed by an array. DelayQueue : An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired.It uses elements that implement the new java.util.concurrent.Delayed interface. PriorityBlockingQueue : This queue bases ordering on a specified Comparator , and the element returned by any take( ) call is the smallest element based on t...

StringBuilder over StringBuffer

As String is an immutable class , any update operations  like replace/substring/append is very expensive. As solution for this StringBuffer class has been introduced for all string update related operations. But problem with StringBuffer is that it is a synchronized class. All its public methods are tagged as “synchronized”. This adds an extra cost to string update operation whenever synchronized is actually not needed. As part of JDK 1.5 a new utility class called StringBuilder has been introduced for non-synchronized string update operations. StringBuilder is designed as a replacement for StringBuffer in single-threaded usage. It means using StrinBuilder instead of StringBuffer where ever synchronization is not an issue will be added to program performance.

Concurrency in Singleton Designing Pattern

      Please refer previous post for Singleton Designing Pattern . Concurrency is the one of the major issues with singleton designing pattern.       There will be multiple client accessing same singleton resource instance. If this resource is mutable then concurrency need to be taken care . Example from previous post is being used here. 1: package demo.singleton; 2: import java.util.concurrent.locks.ReadWriteLock; 3: import java.util.concurrent.locks.ReentrantReadWriteLock; 4: /** 5: * Singleton Resource Class 6: * @author Yogesh 7: */ 8: public class SingletonResource { 9: private static SingletonResource resorce = null ; 10: int count; 11: private ReadWriteLock writeLock = new ReentrantReadWriteLock(); 12: 13: public static SingletonResource getInstance(){ 14: if (resorce == null ){ 15: synchronized (SingletonResource. class ){ 16: if (res...

Understanding volatile keyword in java in details

What is Atomic ? The term atomic is related to the atom, once considered the smallest possible unit. When computer code is considered atomic it can not be interrupted during its execution, We can also define atomic code as code that can’t be found in an intermediate state. Atomicity in Java - Java specifies that basic loading and storing of variable is the atomic. That means the value of the variable can’t be found in a interim state during the store not be changed in the middle of loading the variable to a register. Java Memory Model – Unfortunately , java’s memory model is a bit more complex. Threads are allowed to hold the values of variable in local memory. In that case, when one thread changes the values of the variable, another thread may not see the changed value. This is particularly in loops where looping is controlled by a variable. The lopping may be have already loaded the value of the variable into the register and does not necessary notice when another thread cha...