Posts

Showing posts from June, 2010

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<String> horseList = Collections.synchronizedList( 8:

Map maintaining insert order of key-value pairs

Many time we want to maintain order of entries as it is inserted. HashMap does not satisfy this requirement. For all such requirement java collection framework has provided LinkedHashMap. Following example explains : 1: import java.util.*; 2: class MapTest 3: { 4: public static void main(String[] args) 5: { 6: System.out.println(" Using HashMap: "); 7: Map hashMap = new HashMap(); 8: hashMap.put(" One "," One "); 9: hashMap.put(" Two "," Two "); 10: hashMap.put(" Three "," Three "); 11: hashMap.put(" Four "," Four "); 12: hashMap.put(" Five "," Five "); 13: System.out.println(" Complete Map content : "+ hashMap.toString()); 14: System.out.println(" Iterating Key-Value: "); 15: Iterator itr = hashMap.keySet().iterator(); 16: while (itr.hasNext()){ 17: System.out.println(hashMap.get(itr.next()));

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: System.out.println(" End of Semaphore T

HashMap hashCode collision by example

Image
As we all know Hash is a part of Java Collection framework and stores key-value pairs. HashMap uses hash Code value of key object to locate their possible in under line collection data structure, to be specific it is nothing but array. Hash code value of key object decide index of array where value object get stored. As per hashcode – equals method implementation rules Objects that are equal according to the equals method must return the same hashCode value. & If two objects are not equal according to equals, they are not required to return different hashCode values. As per above statement it is possible that two different object may have same hashcode values, this is called hashcode collision . To overcome this problem concept of bucket has been introduced. All the Value objects whose corresponding Key’s hashcode values are same , they fall under same bucket. Above diagram explains hash code collision. There are three key-value entries are shown , out of which second and thi

getResourceAsStream in details

Image
Here I am putting my analysis for retrieving file as resource stream

Understanding Java Collection Framework

Image
Thought of sharing my understanding about utilizing java collection classes , so here is the post History : Vector & HashTable are introduced very first in JDK 1.0 and then complete collection framework is introduced in JDK 1.2. You can see complete hierarchy of collection framework here . Here i am going to explain their uses in short  

State Design Pattern by Example

Image
State pattern falls under the category of Behavioral patterns. State design pattern comes in picture when a system’s behavior is depends on its own state or some other resource’s state. If an implementation is depends on the states, we end up with snarl of conditional statement every where in code. A very neat approach to deal with such situation is to separate out every state’s behavior and use them where ever they are applicable. The state pattern allows an object to alter its behavior when is internal state changes . The object is appeared to change its class. State pattern is a way to implement the same. Class Diagram : As above class diagram shows, System class holds state instance. As state of system changes , all handle request delegated to respective State’s concrete implementation. Example : Have an example of Vending machine. Vending machine has following states No Coin Inserted Coin Inserted Dispensing Empty Following diagram explain behavior