Posts

Showing posts with the label concurrency

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

CountDownLatch by example

     Java concurrent package provides CountDownLatch class which helps to control execution of set of threads depends on some external activity. CountDownLatch has two main methods countdown() and await().      CountDownLatch’s constructor takes an integer as a parameter which decides its behavior. Calling await() holds execution till countdownLatch’s count(constructor parameter) become zero and countdown() reduces the count on every call.      To explain its use, here is an example of horse race. Here every horse is thread and if you have to stimulate race condition , these threads have to start at a time.  As well as you should know when all complete the race. We can stimulate this functionality using CountDownLatch. 1: import java.util.*; 2: import java.util.concurrent.*; 3: public class TestCountDownLatch 4: { 5: public static void main(String[] args) throws Exception 6: { 7: List...

Explaining Semaphore by example

     This post helps to explore more into java.util.concurrent package. It explains java.util.concurrent.Semaphore class with example. Many time while developing multi threaded application , we may have multiple threads accessing particular system resource concurrently. If you want to limit no of open request for the resource then Semaphore is the option available in java.       Limiting no of open request we can improve throughout of a systemn. Using Semaphore we can control no of running thread at a time out of all available running threads. Following example explains using Semaphore 1: import java.util.*; 2: import java.util.concurrent.*; 3: class SemaphoreTest 4: { 5: public static void main(String[] args) 6: { 7: // Limiting No on threads running to 2 8: Semaphore semaphore = new Semaphore(2); 9: for ( int i=0 ;i<5;i++){ 10: new MyThread(String.valueOf(i),semaphore).start(); 11: } 12: ...

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