Java Interview : Multithreading Part Two

This is the second post in the series Java Interview : Multithreading.
Critical section or a part of code which if processed by multiple threads at a time may have uncalled of consequences, forms the basis of synchronization utilities provided in Java programming language. The initial offering mostly worked around synchronized, wait, notify, notifyAll, sleep, yield and interruption.
In Java, synchronized is used to control access to a piece of code to keep the software behavior on expected lines. This keyword can be applied to a block of code or a method. It works something like this, if thread alpha is executing a piece of code guarded by synchronized, either a method or a block, and another thread beta wants to execute that code too, beta  will be blocked and will need to get the lock to execute that code. Think like a two guys trying to get coffee from a counter, one will get blocked till other is served and if there is a token being circulated, you need to have that token to be served the coffee.
It needs to be made sure to synchronize access to a piece of data everywhere if thats susceptible to corruption. Its generally enough to guard the piece of code than the entire function, as locking unnecessary code might hit the performance. Synchronized keyword in Java takes a object as a parameter and this object serves as a the token or the lock. Generally this  is the object reference passed to it. But you can create exclusive objects and name them appropriately for guarding the code blocks and act as locks.
So if the counter has done with serving coffee and the customer shouts to let one of his friends know that he can rush to get the coffee., or he just shouts out loud to let everyone know that the counter is empty. Thats inter-thread communication. In Java, we use wait()notify() and notifyAll() to doing the above mentioned charade.
The concept of Producer-Consumer rests on the simple notion that the produces will produce and give a shout to the Customer. While its in the process of creating it asks others to wait, once produced the item it shouts out using notify() or notifyAll() to let everyone know that they can get the item.
A lot of utilities are provided after Java 1.5 to make this process flexible and intuitive. The Lock interface provides classes like ReentrantLockand ReentrantLockReadWriteLock which provide a lot of very effective functionalities to effective handle tricky scenarios. You can use lock() in one place and can unlock() at a completely another place, remeber this was not possible using synchronized key word.
The ReentrantLockReadWriteLock class provides pair of associated locks, one for read-only operations and one for writing. Generally a lot of threads just want to read data, there was never a sense in blocking them and needing a lock for such operations. Only one as usual can write or update the data at a time.
Also  as Locks countered the synchronized keyword we needed to get rid of using notify(), wait() etc for this came a new Condition interface which are associated with  the Lock  and give us await(), signal() and signalAll(), a enhanced version of wait(), notify() and notifyAll() methods. You can create them using newCondition() method provided in the Lock interface.
Thats it folks .. wait or await for part three.

Comments

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes