Tuesday, 21 April 2015

Find intersecting node between 2 Linked List

Kind of data structure questions which are normal in C language because C has pointers, so we have address of each memory location. But when we take java into consideration we don't have any pointer to judge whether nodes are same or not except double equal check( i.e. reference check). And then java.util.collections package comes to rescue us.

There are a number of ways by which you can solve the problem but important things are time and space complexity. It matters a lot when you are designing a highly scalable system where performance matters a lot and other side of story is "space comes at premium" which cannot be ignored.

Now let me put the problem statement into diagrammatic representation so that it would be easy to understand.

So here intersecting node is 45 which is first common element in both the linked list.

Time to start with my version of solution which is not very good from space complexity point of view but takes linear time complexity.

Before we start a very good point about IdentityHashMap is that it does reference matching i.e. double equals check not .equals() matching. So we are going to take this advantage in our algorithm.

Alogrithm steps:
1. Create an IndentityHashMap.
2. iterate through list 1 and put all nodes as key into IdentityHashMap.
3. iterate through list 2 and do containsKey check for all nodes in list2. Which ever containsKey returns true is the intersecting node between 2 linked list.

Time Complexity: O(n + m) linear order where n= elements in list1 and m= elements in list2
Space Complexity: O(n) linear order where n = elements in list1.

So far we are done with our algorithm design and need to jump start with code:

1. Below is pojo class which represents individual node in linked list.

 public class Node<T> {  
      private T value;  
      private Node<T> next;  
      public T getValue() {  
           return value;  
      }  
      public void setValue(T value) {  
           this.value = value;  
      }  
      public Node<T> getNext() {  
           return next;  
      }  
      public void setNext(Node<T> next) {  
           this.next = next;  
      }       
 }  

2. Main class where intersection logic written:

 import java.util.IdentityHashMap;  
 import java.util.Map;  
 public class IntersectionExample {  
      public static void main(String[] args) {  
           Node<Integer> list1 = new Node<Integer>();  
           list1.setValue(20);  
           Node<Integer> node1 = new Node<Integer>();  
           node1.setValue(60);  
           Node<Integer> node2 = new Node<Integer>();  
           node2.setValue(90);  
           Node<Integer> node3 = new Node<Integer>();  
           node3.setValue(45);  
           Node<Integer> node4 = new Node<Integer>();  
           node4.setValue(23);  
           node4.setNext(null);  
           list1.setNext(node1);  
           node1.setNext(node2);  
           node2.setNext(node3);  
           node3.setNext(node4);  
           Node<Integer> list2 = new Node<Integer>();  
           list2.setValue(9);  
           Node<Integer> point1 = new Node<Integer>();  
           point1.setValue(5);  
           Node<Integer> point2 = new Node<Integer>();  
           point2.setValue(8);  
           list2.setNext(point1);  
           point1.setNext(point2);  
           point2.setNext(node3);       
           getIntersection(list1, list2);  
      }  
      public static void getIntersection(Node<Integer> list1, Node<Integer> list2){  
           final Integer constant = 1;  
           Map<Node<Integer>, Integer> map = new IdentityHashMap<Node<Integer>, Integer>();  
           while(list1 != null){  
                Node<Integer> current = list1;  
                map.put(current, constant);  
                list1 = list1.getNext();  
           }  
           while(list2 != null){  
                Node<Integer> current = list2;  
                if(map.containsKey(current)){  
                     System.out.println("Intesecting Node, value: "+current.getValue());  
                 break;  
                }  
                list2 = list2.getNext();  
           }  
      }  
 }  

Output:

 Intesecting Node, value: 45  

That's all from my end on this post. As there can be a number of ways by which you can solve this problem, looking forward for your contribution. Thanks.

Monday, 20 April 2015

Difference between new Random() and new Random(long seed) constructor behavior

Once again encountered with an interesting thing which I think need to be shared. This is regarding java.util.Random class to generate random numbers in code base and why 2 different constructors provided in Random class, what behavioral changes we are going to achieve using below 2 constructor:
1. new Random();
2. new Random(long seed); 

So we will start our discussion with code examples which covers above constructors.

Example 1 using new Random() constructor:

 import java.util.Random;  
 public class Example7 {  
      public static void main(String[] args) {  
           Random rand = new Random(); //Random instance without seed  
           for(int i=0; i<10; i++){  
                int randomNumber = rand.nextInt(2);  
                System.out.print(randomNumber+", ");  
           }  
      }  
 }  

Output:
 1, 0, 0, 1, 1, 0, 1, 1, 0, 1,   

(Please note output varies from system to system and execution to execution)

Example 2 using new Random(long seed):


 import java.util.Random;  
 public class Example7 {  
      public static void main(String[] args) {  
           Random rand = new Random(2); //Random instance with seed  
           for(int i=0; i<10; i++){  
                int randomNumber = rand.nextInt(2);  
                System.out.print(randomNumber+", ");  
           }  
      }  
 }  

Output:

 1, 0, 1, 0, 0, 1, 1, 0, 1, 1,   

Now with example 2 you will see that you will get same set of result irrespective of how many time you execute example 2.

But at the same time with example 1 where we used new Random() constructor we will get different set of number on different executions.

Till this we can say that definitely it is "seed" parameter which is responsible for giving same set of random numbers every time.

Time to dig to down to "long seed" parameter to understand the behaviour.

If you check the implementation of default constructor of Random class it uses seed value which will be different for every Random class object creation, where as for parametrized constructor of Random, seed value would be same for each object creation with same seed value.

So no matter how many random objects you create with same seed value, there sequence would be same. Also above statement is true for individual execution also.

If you want go to further into deep, better place would be to check code under Random class. It uses "linear congruential pseudo random number generator" to produce random number..to be honest still trying to understand the algorithm behind it :-)

That's all from my end on Random class. Let me know if you can add something more into it, definitely I am looking forward to update the post.

Friday, 17 April 2015

Smart Java 7 JIT Complier

I don't know about other guys but definitely I used System.currentTimeMillis() to calculate method execution time while doing performance analysis using junits.

This post will cover how things can go completely wrong with such kind of performance testing.

Before we deep dive into the issue let me put a code snippet to explain the issue in better manner:

 public class APSum {  
      public static void main(String[] args) {  
           long then = System.currentTimeMillis();  
           ArithmeticProgression.sum(500000000);  
           long now = System.currentTimeMillis();  
           System.out.println("Elapsed time: " + (now - then));  
      }  
 }  
 class ArithmeticProgression{  
      public static double sum(int i){  
           double sum=0;  
           for(int index=0; index<=i; index++){  
                sum = sum + (double)index;  
           }  
           return sum;  
      }  
 }  

 As we can see in above code, system is calculating sum of numbers starting from zero to provided input number and returning the result. Also we are calculating execution time of method sum using  System.currentTimeMillis().

Now by going through code we can say that total execution time calculation depends on for loop which executed 500000000 time and "sum = sum + (double)index" calculation.

Here our thinking can go wrong with respect to JIT compiler and reason to say that JIT compiler is too much smart.

Let us understand sample of how JIT optimization works:
1. As you can see in main method, we haven't used variable returned from ArithmeticProgression sum method.
2. So when JIT see such kind of code which doesn't mean anything to caller, it just run empty for loop without doing any calculation of "sum = sum + (double)index".

So our assumption of calculating execution time can go completely wrong with the case.

Bottom line is JIT compiler uses a lot of background algorithm to optimize our code while running.

Note:
1. JIT compiler optimization is highly platform dependent so you cannot expect same behaviour across different platform.
2. Scenario is hard to reproduce on local system eclipse as JIT works on compilationThreshold flag of JVM so JIT comes into picture only when compilation reaches over compilationThreshold value.

Sunday, 8 March 2015

Prefer Arrays.asList where you don't need grow able nature of list

Some interesting performance gain which I encountered while writing some test code and thought of sharing with you people.

May be you encountered situation like you have an array in your hand and some method need to be called which takes java.util.List as input parameter. In that case you need to convert that array into list and then call the method. 

Here I am assuming that target method doesn't want to change anything in input list. List content will be used only for method processing.

Now this can be achieved in 2 ways:
1. Create one arrayList, iterate through array and populate arrayList using add method,
2. Using Arrays.asList method to create a fixed size arrayList.

Here second way is far better than first way in terms of performance.

We will take one example to know performance gain achieved using second way.

 package com.example.performance;  
 import java.util.ArrayList;  
 import java.util.Arrays;  
 import java.util.List;  
 public class ListExample {  
      public static void main(String[] args) {  
           int maxNumber = 10;  
           long startTime = System.nanoTime();  
           List<Integer> numbers = new ArrayList<Integer>();  
           for(int i=1; i<=maxNumber; i++){  
                numbers.add(i);  
           }  
           long endTime = System.nanoTime();  
           long timeWithArrayList = endTime - startTime;  
           System.out.println("Time taken to populate array list using add method: "+timeWithArrayList);  
           startTime = System.nanoTime();  
           Integer[] backingArray = new Integer[maxNumber];  
           for(int i=1; i<=maxNumber; i++){  
                backingArray[i-1]=i;  
           }  
           numbers = Arrays.asList(backingArray);  
           endTime = System.nanoTime();  
           long timeWithArraysAsList = endTime - startTime;  
           System.out.println("Time taken to populate array list using Arrays asList method : "+timeWithArraysAsList);  
      }  
 }  

Let us look at result of this example:

 Time taken to populate array list using add method: 786449  
 Time taken to populate array list using Arrays asList method : 199285  
  
As you can see Arrays.asList took 1/3rd time when compared against list.add method.

(Note: processing time will vary, but asList method will always take less time when compared to add method.)

It is time to know the reason why add method takes more time if compared against Arrays.asList method.

If you check implementation of ArrayList add method, for each add method call array list checks for capacity using ensureCapacity method. So this is an overhead if you want fixed size list for some specific purpose.

That's all from my end on this topic. Looking forward for your suggestions and comments.


Sunday, 22 February 2015

Java Serialization Part 1

I would not be shy away to say that I feel difficulty while questions or coding from java IO package comes in front of me. May be the reason is I haven't got that much coding exposure of IO operations. So things become confusing when we go into depth of java IO package.

Java serialization is also a small bit of that Java IO package, where our understanding is less.

I remember 2-3 years before one of my friend asked for real time usage of serialization and I told him that we can persist objects in a flat file instead of database. May be that was somehow correct answer but he was not convinced with that.

Today I am pretty much comfortable to answer his questions with example of EJB remote beans or high speed trading application where things cannot work without serialization.

Also mostly people get stuck when they have been asked to write a code which can serialize an object. 

So here I am going to share some real time example of serialization usage and code example related to serialization.

As I told earlier here are some real time example where serialization comes into picture:

1. May be few of you have worked on EJB and used it's remote bean. Now by saying any java class as remote bean, we mean that the class is available in some other JVM and client which want to invoke method of that remote bean exists in some other JVM. 
      Suppose if that method invocation need some object as parameter then somehow the object need to be passed from one JVM to other JVM via network. As network doesn't understand anything else 0 and 1, so this is done using serialization. There is complete cycle of java object serialization and de-serialization while invoking EJB remote method.

2. My next example will be stock exchange high speed trading application. As we know share price changes in fraction of seconds, so delay of millisecond in placing order will affect purchase cost tremendously. Trading application cannot go for relational database to store live trading transactions because of latency associated with it. Such kind of applications heavily rely on memory mapped file where serialization comes into picture.

Below picture can better explain definition of Serialization rather than words.


   


Code example for Serialization:

Here is my version of Serialization utility which can serialize and de-serialize an object:

 import java.io.FileInputStream;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.io.ObjectInputStream;  
 import java.io.ObjectOutputStream;  
 import java.util.Arrays;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 public class SerializationUtils<E> {  
      Logger logger = Logger.getLogger("SerializationUtils.class");  
      private String filePath;  
      public SerializationUtils(String filePath){  
           this.filePath = filePath;  
      }  
      public void writeObject(E object){  
           FileOutputStream fos=null;   
           ObjectOutputStream oos= null;   
           try{  
                fos = new FileOutputStream(this.filePath);  
                oos = new ObjectOutputStream(fos);  
                oos.writeObject(object);  
           }catch(FileNotFoundException fnfe){  
                logger.log(Level.INFO, filePath+" not found");  
           }catch(IOException ioe){  
                logger.log(Level.SEVERE, Arrays.toString(ioe.getStackTrace()));  
           }finally{  
                try{if(null != oos)oos.close();}catch(IOException ioe)  
                     {logger.log(Level.SEVERE, Arrays.toString(ioe.getStackTrace()));}  
           }  
      }  
      @SuppressWarnings("unchecked")  
      public E readObject(){  
           FileInputStream fis = null;  
           ObjectInputStream ois = null;  
           E e = null;  
           try{  
                fis = new FileInputStream(this.filePath);  
                ois = new ObjectInputStream(fis);  
                e = (E)ois.readObject();  
           }catch(ClassNotFoundException cnfe){  
                logger.log(Level.INFO, Arrays.toString(cnfe.getStackTrace()));  
           }catch(FileNotFoundException fnfe){  
                logger.log(Level.INFO, filePath+" not found");  
           }catch(IOException ioe){  
                logger.log(Level.SEVERE, Arrays.toString(ioe.getStackTrace()));  
           }finally{  
                try{if(null != ois)ois.close();}catch(IOException ioe)  
                {logger.log(Level.SEVERE, Arrays.toString(ioe.getStackTrace()));}  
           }  
           return e;  
      }  
 }  

Now let us use this utility to serialize an object of employee class object.

Employee class(must implement Serializable interface):

 import java.io.Serializable;  
 public class Employee implements Serializable{  
      private static final long serialVersionUID = 1L;  
      private String employeeId;  
      private String name;  
      public String getEmployeeId() {  
           return employeeId;  
      }  
      public void setEmployeeId(String employeeId) {  
           this.employeeId = employeeId;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      @Override  
      public String toString() {  
           return "Employee [employeeId=" + employeeId + ", name=" + name + "]";  
      }  
 }  

Main class using serialization for object of employee class:

 public class SerializationTest {  
      public static void main(String[] args){  
           SerializationUtils<Employee> utils = new SerializationUtils<Employee>("E:\\WrkSpace\\Test\\serialized_object.txt");  
           Employee emp = new Employee();  
           emp.setEmployeeId("123");  
           emp.setName("shashi");  
           utils.writeObject(emp);  
           System.out.println("done with serialization");  
           Employee deserializedEmployee = utils.readObject();  
           System.out.println(deserializedEmployee.toString());  
      }  
 }  

Here is the output which comes in console as expected:

 done with serialization  
 Employee [employeeId=123, name=shashi]  

If you are curious about what have been written in flat file, here it is:


That's all from my end in this part of serialization. We will cover more of serialization in second part where we will override standard writeObject and readObject method of ObjectOutputStream and ObjectInputStream respectively.



Wednesday, 18 February 2015

Find second highest number in an array

Nowadays technical interview involves writing small small code to judge whether candidate is aware of java APIs or not and how he/she using APIs in different scenarios.

For any such scenario, there can be 10 ways in which you can code a given problem but among those only 2-3 ways will solve the problem efficiently.

Take an example to "find out second highest number in a given array."

My version of solving this problem statement would be using stack with size 2. It will hold highest and second highest number by traversing on given array.

Pseudo Code:
1. Create a stack with size limit 2.
2. Start traversing on given array arr[n] from i=0 to n-1, step 1.
3. If arr[i] is greater than HEAD element of stack then push arr[i] into stack, so that HEAD element       will go one level down and arr[i] will become HEAD.
    Else if arr[i] is greater than TAIL element of stack then replace TAIL element with arr[i].

This diagram will be helpful to understand stack push operation:



As we know that Stack class of Java has no size limitation so somehow we have to limit the size to 2. Therefore I created customized stack to store elements. As you can see I overridden push method and if stack size becomes equal to max size(in our case its 2), then I am removing bottom element before doing any push operation to stack.

 import java.util.Stack;  
 @SuppressWarnings("serial")  
 public class SizedStack<E> extends Stack<E> {  
      private int maxSize;  
      public SizedStack(int maxSize){  
           super();  
           this.maxSize = maxSize;  
      }  
      @Override  
      public E push(E item){  
           if(this.size() == maxSize){  
                this.remove(0);  
           }  
           return super.push(item);  
      }  
 } 


Class which is implementing discussed pseudo code:

 public class FindSecondLargest {  
      public static void main(String[] args) {  
           int[] arr = {4,3,2,9,4,5,3,7};  
           System.out.println(getSecondLargest(arr));  
      }  
      public static int getSecondLargest(int[] arr){  
           SizedStack<Integer> stack = new SizedStack<Integer>(2);  
           int head = stack.push(0);  
           int tail = stack.get(0);  
           for(int i=0; i<arr.length; i++){  
                if(arr[i] > head){  
                     head = stack.push(arr[i]);  
                }else if(arr[i] > tail){  
                     tail = stack.set(0, arr[i]);  
                }  
           }  
           return stack.get(0);  
      }  
 }  

As you can see that each element in the array will be compared against 2 elements at most which resides in stack.

Since this operation is repeated exactly n times(size of array), hence we can say that time complexity of this algorithm will be O(n).

At the same time I cannot say that my version is best approach for given problem statement, but one of efficient way to solve the problem.

If you can give some solution and time complexity in terms of some logarithmic function then definitely your solution would be more efficient that mine version.


Saturday, 14 February 2015

Prefer Primitive over Wrapper classes

I was going through "Effective Java" book by Joshua Bloch and encountered a very interesting fact discussed on performance. So I thought, I must share this with you people.

It is about how you can optimize your code tremendously by just using primitive variable.

Here we are going to do the same thing in 2 different program, one using Wrapper Long class and one using primitive long.

We will take an example of doing sum from 1 to Integer.MAX_VALUE and analyze total execution time.

Example 1: Sum using wrapper Long class.

 import java.util.concurrent.TimeUnit;  
 public class LongTest {  
      public static void main(String[] args) {  
           Long sum= new Long(0);  
           long startTime = System.nanoTime();  
           for(long i=0; i<Integer.MAX_VALUE;i++){  
                sum+=i;  
           }  
           long endTime= System.nanoTime();  
           System.out.println(sum+" in "+TimeUnit.SECONDS.convert(endTime-startTime, TimeUnit.NANOSECONDS)+" seconds");  
      }  
 }  

If you execute above code snippet, total execution time will come around approximately 7 second.


Example 2: Sum using primitive long

 import java.util.concurrent.TimeUnit;  
 public class LongTest {  
      public static void main(String[] args) {  
           long sum=0;  
           long startTime = System.nanoTime();  
           for(long i=0; i<Integer.MAX_VALUE;i++){  
                sum+=i;  
           }  
           long endTime= System.nanoTime();  
           System.out.println(sum+" in "+TimeUnit.SECONDS.convert(endTime-startTime, TimeUnit.NANOSECONDS)+" seconds");  
      }  
 }  

If you execute above code snippet, total execution time will come around approximately 1 second.

Example 2 differs from example 1 just by variable declaration. It uses primitive long instead of Wrapper Long class.

So bottom line is by just using primitives, we saw huge performance gain. Here it is 6 seconds which is huge difference.

Conclusion: Always try to use primitive as much as possible over Wrapper class, because that reduces unnecessary overhead of auto boxing and un-boxing from JVM end.

Friday, 23 January 2015

Insertion Sort with example

Data Structure and Algorithm:

As we all know that "Data Structure and Algorithm" plays crucial role in computer science.

But if Java already provided a number of classes and APIs under collection framework to deal with it, why would we have to care about internal implementation.

After all Java is all about abstraction.


For example take Collections.sort() method which sorts elements as per natural or custom sorting order passed to method.

So why should we worry about internal implementation of Collections.sort() as below:
1. convert list to an array.
2. pass that array to Arrays.sort() method which uses merge sort algorithm to sort elements.
3. convert back sorted array to list.  

Well human civilization works on improvement. Who knows tomorrow you have some sorting algorithm which sorts elements faster than Arrays.sort() method with less memory requirement.

At the same by understanding internal implementation of data structure and its algorithm, we would be in better position to decide which data structure to be used in specific scenario to get optimum trade off between time and space.

As "Data Structure and Algorithm" is very vast topic, so it is not possible for me to cover everything about it with my small knowledge.

In this post, we will go through one of sorting algorithm called "Insertion Sort", its basic understanding and code implementation.

Insertion Sort:

Take real time example of playing cards. You arrange cards in your hand with respect to suits and ascending order.
So suppose you have 4 cards in you hand {4, 7, 8, J} and if next card is 5 then you put that card in between 4 and 7.

That's how Insertion Sort works. It says put the elements at its desired location as per order.

Pseudo code:

for i = 1 to length(A) - 1
    j = i
key = A[i]
    while j >= 0 and A[j-1] > key
        swap key and A[j-1]
        j = j - 1

Idea is, at end of every iteration all elements before nth element should be in sorted order. 

Here n represents position of key node which need to be put in sorted array.

Below diagram can explain in better manner:

Code:
 import java.util.Arrays;  
 public class InsertionSortExample {  
      public static void main(String[] args) {  
           int[] arr = {6,5,3,1,8,7,2,4};  
           InsertionSortExample sortUtil = new InsertionSortExample();  
           sortUtil.sort(arr);  
           System.out.println(Arrays.toString(arr));  
      }  
      public void sort(int[] arr){  
           int key;  
           for(int i=1; i<=arr.length-1; i++){  
                key = arr[i];  
                int j = i-1;  
                while(j>=0){  
                     if(key < arr[j]){  
                          arr[j+1] = arr[j];  
                          j--;  
                     }else{  
                          break;  
                     }  
                }  
                arr[j+1] = key;  
           }  
      }  
 }  


That's all from my end about Insertion Sort. Please let me know if you find any issues with topic and its explanation.


Sunday, 18 January 2015

Insight of BlockingQueue in Java -- ArrayBlockingQueue vs LinkedBlockingQueue vs PriorityBlockingQueue

Producer Consumer Design Pattern:

As usual, before we start with use cases of BlockingQueue and its API, let us go through some real time problems. This will help us to better understand use cases of BlockingQueue.

We are part of generation, who saw an era of postal department. The time when communication medium were inland letters and postal cards. Drastic changes came in communication system from Postal Card to STD booth to Pager to Cell phones.

Continuing to the story of postal department, if we have to send a letter to our relatives we have to write down complete recipient address on top of letter and drop it to near by postal box. As per schedule postman used to come and collect those letters/postal card from box and take it to postal office for further journey.

Now if we look at the above scenario in terms of process, your work(e.g. writing and sending postal card) don't have any dependency on post man. You dropped letter into post box and assumes that it will reach destination safely without worrying about when post man will come to collect, what is his name, where he/she will take that letter.

So in complete story post box acts as "working queue", the person who dropped letter is "producer" and post man who collects those letter is "consumer".

Suppose if we remove "post box" from the scenario, then condition would be like we have to inquire when post man usually comes, wait for post man until he/she comes and then give letter by hand.  

Simply by having a "post box", reduces effort at producer side as well as consumer side for complete process.

Here the story of "Producer Consumer Design Pattern" comes to end and we are pretty clear on how this design pattern comes handy to resolve complex real time scenario.

This is good starting point to look at implementations that Java provide for "Producer Consumer Design Pattern".

BlockingQueue is one of interface Java provides to solve problems in Producer Consumer Pattern.

Important points on BlockingQueue:
1. This is an interface added in Java 5 as part of concurrency framework.
2. There are multiple implementations of BlockingQueue like ArrayBlockingQueue,              LinkedBlockingQueue, PriorityBlockingQueue etc .

To understand any framework, the best way is to look at the interfaces provided by that framework.
Classes   are just mere implementation of those interfaces. So if you are done with interfaces itself, classes are very easy to understand.

4. BlockingQueue provides 2 types of methods.
One is blocking methods like put and take which blocks the thread if queue is full and empty respectively. Another flavor is non blocking(timed equivalent) methods like offer and poll
in which method returns false to requesting thread if the specified waiting time elapses before space is available in queue.

5. BlockingQueue can be of bounded or unbounded type.
In case of bounded BlockingQueue(e.g. ArrayBlockingQueue), once capacity exhausted no thread can put task in work queue.For unbounded BlockingQueue (e.g. LinkedBlockingQueue), specifying bound is optional. The capacity, if unspecified, is equal to Integer.MAX_VALUE.

Some Points and Example of ArrayBlockingQueue:
1. A bounded blocking queue which is backed by an array.
2. The head of the queue is that element that has been on the queue the longest time.
3. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.

 import java.util.Iterator;  
 import java.util.concurrent.ArrayBlockingQueue;  
 import java.util.concurrent.BlockingQueue;  
 public class ArrayBlockingQueueExample {  
      public static void main(String[] args) throws InterruptedException {  
           BlockingQueue<String> workQueue = new ArrayBlockingQueue<String>(2);  
           ProducerTask task1 = new ProducerTask(workQueue,  
                     "create simple java project");  
           ProducerTask task2 = new ProducerTask(workQueue,  
                     "create package structure");  
           ProducerTask task3 = new ProducerTask(workQueue, "create java class");  
           Thread prodThread1 = new Thread(task1);  
           Thread prodThread2 = new Thread(task2);  
           Thread prodThread3 = new Thread(task3);  
           prodThread1.start();  
           prodThread2.start();  
           prodThread3.start(); // thread will go into blocked status as queue size is only 2  
           // checking work queue for queued tasks  
           System.out.println("Checking status of queue, before any consumer thread picked task from queue");  
           Iterator<String> workQueueItr = workQueue.iterator();  
           while (workQueueItr.hasNext()) {  
                System.out.println("Queued task in work queue: "+ workQueueItr.next());  
           }  
           System.out.println("*************************************************************************");  
           ConsumerTask task4 = new ConsumerTask(workQueue);  
           Thread consTask4 = new Thread(task4);  
           consTask4.start();  
           consTask4.join();  
           // checking work queue for queued tasks  
           System.out.println("Checking status of queue, after a consumer thread picked a task from queue");  
           workQueueItr = workQueue.iterator();  
           while (workQueueItr.hasNext()) {  
                System.out.println("Queued task in work queue: "+ workQueueItr.next());  
           }  
           System.out.println("*************************************************************************");  
      }  
 }  
 class ProducerTask implements Runnable {  
      private BlockingQueue<String> workQueue;  
      private String taskName;  
      public ProducerTask(BlockingQueue<String> workQueue, String taskName) {  
           this.workQueue = workQueue;  
           this.taskName = taskName;  
      }  
      @Override  
      public void run() {  
           try {  
                workQueue.put(taskName);  
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
      }  
 }  
 class ConsumerTask implements Runnable {  
      private BlockingQueue<String> workQeue;  
      public ConsumerTask(BlockingQueue<String> workQueue) {  
           this.workQeue = workQueue;  
      }  
      @Override  
      public void run() {  
           try {  
                String taskName = workQeue.take();  
                System.out.println("Task taken from work queue: " + taskName);  
                System.out.println("*************************************************************************");  
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
      }  
 }  

If you check results you will understand that 3rd producer thread got blocked and unable to put task in work queue until some consumer thread came and pick one task from work queue.

Result: 
Checking status of queue, before any consumer thread picked task from queue
Queued task in work queue: create simple java project
Queued task in work queue: create package structure
*************************************************************************
Task taken from work queue: create simple java project
*************************************************************************
Checking status of queue, after a consumer thread picked a task from queue
Queued task in work queue: create package structure
Queued task in work queue: create java class
*************************************************************************


Some Points and Example of LinkedBlockingQueue:
1. It is an optionally bounded blocking queue based on linked nodes.
2. The head of the queue is that element that has been on the queue the longest time.
3. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.
4. Capacity, if unspecified, is equals to Integer.MAX_INT.


 import java.util.Random;  
 import java.util.concurrent.BlockingQueue;  
 import java.util.concurrent.LinkedBlockingQueue;  
 public class LinkedBlockingQueueExample {  
      public static void main(String[] args) {  
           final BlockingQueue<String> workQueue = new LinkedBlockingQueue<String>(10);  
           Random randomGenerator = new Random();  
           ProducerTask prodTask = new ProducerTask(workQueue, randomGenerator);  
           Thread prodThread = new Thread(prodTask);  
           prodThread.start();  
           ConsumerTask consTask = new ConsumerTask(workQueue);  
           Thread consThread = new Thread(consTask);  
           consThread.start();  
      }  
 }  
 class ProducerTask implements Runnable {  
      private BlockingQueue<String> workQueue;  
      private Random randomGenerator;  
      public ProducerTask(BlockingQueue<String> workQueue, Random randomGenerator) {  
           this.workQueue = workQueue;  
           this.randomGenerator = randomGenerator;  
      }  
      @Override  
      public void run() {  
           try {  
                while(true){  
                     String taskNumber = randomGenerator.nextInt(1000)+"";  
                     System.out.println("putting task no: "+taskNumber+" in work queue");  
                     workQueue.put(taskNumber);  
                     System.out.println("*************************************************************************");  
                     Thread.sleep(200);  
                }                 
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
      }  
 }  
 class ConsumerTask implements Runnable {  
      private BlockingQueue<String> workQeue;  
      public ConsumerTask(BlockingQueue<String> workQueue) {  
           this.workQeue = workQueue;  
      }  
      @Override  
      public void run() {  
           try {  
                while(true){  
                     String taskNumber = workQeue.take();  
                     System.out.println("Task taken from work queue: " + taskNumber);  
                     System.out.println("*************************************************************************");  
                     Thread.sleep(500);  
                }                 
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
      }  
 }  


Result:

putting task no: 403 in work queue
*************************************************************************
Task taken from work queue: 403
*************************************************************************
putting task no: 496 in work queue
*************************************************************************
putting task no: 446 in work queue
*************************************************************************
Task taken from work queue: 496
*************************************************************************
putting task no: 967 in work queue
*************************************************************************
putting task no: 405 in work queue
*************************************************************************
putting task no: 994 in work queue
*************************************************************************
Task taken from work queue: 446
*************************************************************************

Some Points and Example of PriorityBlockingQueue:
1. An unbounded blocking queue, uses same ordering rules as java.util.PriorityQueue class.
2. null elements are not permitted.
3. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so results in ClassCastException).
4. All elements inserted into the PriorityBlockingQueue must implement the java.lang.Comparable interface.



 public class PriorityBlockingQueueExample {  
      public static void main(String[] args){  
           BlockingQueue<String> workQueue = new PriorityBlockingQueue<String>();  
           try {  
                workQueue.put("A");  
                workQueue.put("C");  
                workQueue.put("F");  
                workQueue.put("E");  
                workQueue.put("D");  
                workQueue.put("B");  
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
           //checking queue after all puts whether tasks are queued as per natural ordering or not  
           while(true){  
                try {  
                     System.out.println("Task name: "+workQueue.take());  
                } catch (InterruptedException e) {  
                     e.printStackTrace();  
                }  
           }  
      }  
 }  

Result:
As you can see in result that all task are order as per natural ordering of String.
Task name: A
Task name: B
Task name: C
Task name: D
Task name: E
Task name: F

That's all from my end on Producer Consumer Desing Pattern and BlockingQueue in Java.

Friday, 16 January 2015

Understanding Builder Design Pattern

Before starting with Builder Design pattern concept and how we can implement this in java, let me first take you to issues which we face in our code practice.

This would help us to connect better with learning of Builder Design pattern.

Take an example of alloy composition(e.g. stainless steel). Alloy composition is built on a number of organic complex elements in predefined percentage(by mass).

So quality of an alloy depends on percentage of building elements added during process. We can take example of Ashok Stambh, New Delhi which was built during 3rd Century BC and still no sign of rust on it :-)

Now if we have to write down "Alloy" class in java that would be something like below:

 public class Alloy{  
      private float iron;  
      private float chromium;  
      private float molybdenum;  
      private float carbon;  
     public Alloy() {  
           super();  
      }  
      public Alloy(float iron, float chromium, float molybdenum, float carbon) {  
           super();  
           this.iron = iron;  
           this.chromium = chromium;  
           this.molybdenum = molybdenum;  
           this.carbon = carbon;  
      }  
      public float getIron() {  
           return iron;  
      }  
      public void setIron(float iron) {  
           this.iron = iron;  
      }  
      public float getChromium() {  
           return chromium;  
      }  
      public void setChromium(float chromium) {  
           this.chromium = chromium;  
      }  
      public float getMolybdenum() {  
           return molybdenum;  
      }  
      public void setMolybdenum(float molybdenum) {  
           this.molybdenum = molybdenum;  
      }  
      public float getCarbon() {  
           return carbon;  
      }  
      public void setCarbon(float carbon) {  
           this.carbon = carbon;  
      }  
 }  


Say our client code want to create an instance of Alloy class so there can be 2 ways to do:

1. call default constructor of Alloy class and then set all instance variables by setter of Alloy class.
2. Other way can be call parameterized constructor, pass all user inputs in one go and create instance of Alloy class.

Here we have only 4 instance variable in Alloy class, but suppose if a class has more than 10 instance variables.

In that case calling setter for all instance variables in client code will be not look good as it increases line of code. At the same time calling parameterzied constructor with all 10 inputs will lead to programming error, where programmer passed wrong values in constructor.

Here Builder Design pattern comes into picture to rescue us.

Let us modify the existing Alloy class and see how builder pattern overcome with above mentioned 2 issues.

 public class Alloy {  
      private float iron;  
      private float chromium;  
      private float molybdenum;  
      private float carbon;  
      private Alloy(Builder builder){  
           this.iron = builder.iron;  
           this.chromium = builder.chromium;  
           this.molybdenum = builder.molybdenum;  
           this.carbon = builder.carbon;  
      }  
      public static class Builder{  
           private float iron;  
           private float chromium;  
           private float molybdenum;  
           private float carbon;  
           public Builder(){  
                super();  
           }  
           public Builder iron(int val){  
                iron = val;  
                return this;  
           }  
           public Builder chromium(int val){  
                chromium = val;  
                return this;  
           }  
           public Builder molybdenum(int val){  
                molybdenum = val;  
                return this;  
           }  
           public Builder carbon(int val){  
                carbon = val;  
                return this;  
           }  
           public Alloy build(){  
                return new Alloy(this);  
           }  
      }  
 }  

Here our intention it to first create basic object and build required object on top of that. So we introduced an inner class say Builder which will take the responsibility of building object of Alloy class.


Let see how code looks like in client end who wants to create instance of  Alloy class

 Alloy stainlessSteel = new Alloy.Builder().iron(30).chromium(12)  
                                                   .molybdenum(20).carbon(40).build();  

As you can see, how we are creating an instance of Alloy class in a meaningful way that too with less line of codes. There is no whole bunch of setters or passing a number of parameters in parameterized constructor.

At last formal definition of "Builder Design Pattern" (wiki):
Builder Design pattern is to find a solution to the telescoping constructor anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object constructor parameter combination leads to an exponential list of constructors. Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

Suggestions: I would suggest book "Effective Java 2" by Joshua Bloach who explained Builder Design Pattern in simple but effective way.