Thursday 29 October 2015

Fail- fast iterator and Fail-safe iterator


Java iterator provides us with interface to parse the items of the underlying collection.
When we are using the iterator the underlying collection should not be modified.
If this treaty is not honoured and it is possible in a multi-threaded environment, then we get a ConcurrentModificationException.

What is the difference between Fail- fast iterator and Fail-safe iterator ?

Fail fast : it may fail ... and the failure condition is checked aggressively so that the failure condition is detected before damage can be done.

 This behaviour is not guaranteed.
 Detecting modification in the collection and parsing the collection is not done synchronously.
 Modification on the collection may go unnoticed under certain circumstances.
 So while programming, this behaviour should not be banked upon. Example for fail fast iterators are ArrayList, Vector, HashSet.

 fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis.

fail safe : it won't fail.
 iterator doesn't throw any Exception if Collection is modified structurally while one thread is Iterating over it because they work on clone of Collection instead of original collection and that’s why they are called as fail-safe iterator.
Iterator of CopyOnWriteArrayList is an example of fail-safe Iterator also iterator written by ConcurrentHashMap keySet is also fail-safe iterator and never throw ConcurrentModificationException in Java.

 Even if the underlying collection is modified, it does not fail by throwing ConcurrentModificationException.
 When an iterator is created, either it is directly created on the collection, or created on a clone of that collection. One example which supports failsafe iterator is ConcurrentHashMap.


 ConcurrentModificationException : Fail Fast and Fail Safe iterators
The ConcurrentModificationException is thrown if the Collection is modified while Iterating over the data structure.
We know that the collection represents a set of objects for which it manages an internal data structure (i.e. an Object array). Now when we want to iterate over the collection of objects. We have two approaches:

1. Fail Fast : In this case the iterator refers the internal data structure directly (i.e. object array) reading the objects. Here it considers the underlying data structure (i.e. the object[]) is not modified while iterating through the collection. To do this it manages an internal flag which is set on any modification to the data structure (like add / remove), and every time we call the iterator to get the next element it checks the flag. If it finds the data was modified after this iterator was created, it throws exception; ConcurrentModificationException.

2. Fail Safe : In this case the iterator will make a copy of the internal data structure and iterates over the copied data structure. Thus any modifications done to the internal data structure will not effect the iterator. In this case we dont find ConcurrentModificationException. But in this approach we can find the following two issues:
a. The overhead of copying the data structure (time and memory)
b. The fail safe iterator doesnt guarantees the data being read is the data currently in the data structure with the created collection.
The java.util.Iterator is implemented using fail fast approach. That means using the Iterator (i.e. the iterator() method of List) will not result in creating a copy of internal object array but will reset the flag.
The java.util.Enumeration is a fail safe approach implemented. That means using the Enumeration (like elements() method of Vector) results to create a copy of internal data structure.

Fail fast or fail safe – which is better?

fail fast is best.


a. Fail-fast throw ConcurrentModificationException while Fail-safe does not.
b. Fail-fast does not clone the original collection list of objects while Fail-safe creates a copy of the original collection list of objects.

Monday 26 October 2015

Generics

The Java Generics features were added to the Java language from Java 5.

Generic in Java is added to provide compile time type-safety of code and removing risk of ClassCastException at runtime which was quite frequent error in Java code, for those who doesn’t know what is type-safety at compile time, it’s just a check by compiler that correct Type is used in correct place and there should not be any ClassCastException.


There are mainly 3 advantages of generics. They are as follows:

1) Type-safety : We can hold only a single type of objects in generics. It doesn’t allow to store other objects.

2) Type casting is not required: There is no need to typecast the object.

3)  Compile-Time Checking: It is checked at compile time so problem will not occur at runtime. The good programming strategy says it is far better to handle the problem at compile time than runtime.

Friday 16 October 2015

Synchronization


Synchronization in Java?

Synchronization in java is the capability to control the access of multiple threads to any shared resource.

Java Synchronization is better option where we want to allow only one thread to access the shared resource.

Why use Synchronization?

The synchronization is mainly used to

To prevent thread interference.
To prevent consistency problem.

Types of Synchronization

There are two types of synchronization

Process Synchronization
Thread Synchronization



Synchronized block in java

Synchronized block can be used to perform synchronization on any specific resource of the method.

main point :

Synchronized block is used to lock an object for any shared resource.
Scope of synchronized block is smaller than the method.

Synchronized method

Synchronized method is used to lock an object for any shared resource.

Difference between synchronized method vs block in Java

1.One significant difference between synchronized method and block is that, Synchronized block generally reduce scope of lock. As scope of lock is inversely proportional to performance, its always better to lock only critical section of code.

2.Synchronized block provide control over lock, as you can use arbitrary lock to provide mutual exclusion to critical section code. On the other hand synchronized method always lock either on current object represented by this keyword  or class level lock, if its static synchronized method.

3. Synchronized block can throw throw java.lang.NullPointerException.

4. In case of synchronized method, lock is acquired by thread when it enter method and released when they leaving method, either normally or by throwing Exception.
On the other hand in case of synchronized block, thread acquires lock when they enter synchronized block and release when they leave synchronized block.

The difference is in which lock is being acquired:

synchronized method acquires a method on the whole object. This means no other thread can use any synchronized method in the whole object while the method is being run by one thread.

synchronized blocks acquires a lock in the object between parentheses after the synchronized keyword. Meaning no other thread can acquire a lock on the locked object until the synchronized block exits.

So if you want to lock the whole object, use a synchronized method. If you want to keep other parts of the object accessible to other threads, use synchronized block.

If you choose the locked object carefully, synchronized blocks will lead to less contention, because the whole object/class is not blocked.

This applies similarly to static methods: a synchronized static method will acquire a lock in the whole class object, while a synchronized block inside a static method will acquire a lock in the object between parentheses.