Java Interview : Multithreading Part Three
Part three of Java Interview : Multi-threading Java 1.5 onwards a lot of useful utility classes tailor made to counter a lot of specific scenarios were introduced, i will write about some of the commonly used ones. Classes like Semaphore, CountDownLatch, CyclicBarrier, Phaser, Exchanger and the Executor Framework are the most famous ones. Semaphore class is modeled around the concept of the counting semaphore, where a set of permits are maintained. We have the acquire() method which tries to get a permit and blocks until it gets one. The release() method puts the permit back to the pool, here by increasing the permit count. Methods like tryAcquire() provide non-blocking alternative to get the lock. Binary semaphores have only one permit and thus a thread can either be in one state on or off at a single time. CountDownLatch is a utility that provides methods like await(), basically signalling the threads to wait at a point till the count comes down to 0, before proceedi...