Saturday, September 21, 2013

Intrinsic Locks and Synchronization

What is Synchronization ?
Synchronization is a way to control the access to a shared resource in multithreaded environment in such a way that only one thread can access a shared resource at a time. Synchronization is achieved through the concept of monitor. A monitor is an exclusive lock that is obtained on an object by a thread when a synchronized method is called on the object ( or a synchronized block is executed ). 


Intrinsic lock / Monitor lock.
Intrinsic lock or monitor lock plays an important role in different aspects of synchronization :
  • It enforces exclusive access to object's state .
  • It establishes Happens-before relationship that is essential for visibility .

Syntax.
synchronized keyword is used to denote that a particular block of code is to be executed in a monitor. Synchronization can be done in two ways :

1. Synchronized methods .
synchronized keyword can be used with both instance methods and static methods .
// Synchronizing instance methods . Lock will be taken on the current object i.e object used to call the method doStuff().
public synchronized void doStuff(){
    // Statements to be executed in monitor.
}

// Synchronizing static methods . Lock will be taken on class instance ( java.lang.Class ) 
public static synchronized void doSomeExtra(){
    // Statements to be executed in monitor.
}


2. Synchronized statements.

It is useful for improving concurrency with fine grained synchronization.

// Synchronizing blocks . Lock will be taken on the current object i.e object used to call the method doStuff().
public void doStuff(){
    // Statements that are not executed in monitor.
    synchronized(this){
    // Statements to be executed in monitor.
    }
}

Reentrant synchronization 
Allowing a thread to acquire a lock more than once is called Reentrant synchronization. This can happen in a scenario where one synchronized code , directly or indirectly invokes a method that also contains synchronized code , and both the code use the same lock . Without reentrant synchronization , synchronized code would have to take many additional precautions to avoid having a thread cause itself to block . 

Limitations of Synchronization 
Synchronization can only make sure that a shared object can be accessed by one thread at a time . It can not determine order of the usage . Sometimes there is a need to use a common resource in a specific order by multiple threads .

Race condition in Concurrency

The situation where multiple threads try to operate on a shared resource without proper synchronization  and sequence of their operations interleave is  Race Condition ( Also see Thread interference and Memory consistency errors) .

Example 1 .
The most basic example to describe a Race condition is a Counter class  that increments value of an instance variable . 
class Counter {  
    private int c = 0;  
  
    public void increment() {  
        c++;  
    }  
}
Note :
It seems that the increment operation is atomic ( Atomic operations can't be interleaved by threads ) but it is not . It can further be divided into three steps : 
1. Read the value c. 
2. Increment the value . 
3. Write the incremented value.


Lets imagine that two threads A and B increments the value of instance variable c . Ideally if there is no interleaving of thread's operations , the value of the variable should be 2 as both the threads increment it by 1 . Lets see one of the possible scenario where thread's operations interleave 

Thread A : Read  c ( which is 0 ).
Thread B : Read  c ( Still 0 ).
Thread A : Increments the value by 1.
Thread A : Write the value ( Now c is 1 )
Threab B : Increments the value  
Thread B : Write the value ( Still c is 1 ) 

Here we can see that due to interleaving of Read operations of two threads , the final value of the variable c is 1 and not 2. 

Example 2 .
Below is a short snippet from a singleton class . Here first the code checks whether the instance is null and then creates a new object . The purpose of this code is to ensure that there is only one instance of MySingleton class. 
public MySingleton getInstance(){
   if (instance == null){
       instance  = new MySingleton();
   }
}
Now lets consider that there are two threads trying to get an instance of MySingleton class by calling the method getInstance() . It might be possible , due to interleaving of thread's operations , that both the thread sees the value of instance as null  and in that case , two objects will get created by the threads . 

How to fix the problem ?
Race conditions can be avoided by proper synchronization of the code . As an application developer , you have to enforce locking to make sure that only one thread enters a critical section at a time  and the result of the  thread is visible to others once it  comes out of the synchronized block .


Saturday, September 14, 2013

OOP concept in Java


Hi Guys !!! So this blog is all about the Object Oriented Programming  concepts . Lets start from the very basic concept of Object and Class .

What is an Object ?
Well It is the most basic concept and a key to understand the Object-Oriented programming . Object is an entity that has two characteristics ,  State and Behavior . Some examples of real world object can be : Bike , Chair , Dog etc. 

Lets take an example of a Bike . Bike has some state ( current gear , current speed ) and behavior ( change gear , apply brake ) . One way to begin thinking in an object-oriented way is to identify the state and behavior of real world objects .

Software objects are also similar to the real world objects. They too contains State and Behavior . An Object stores its state in  fields and exposes the behavior through methods.



What is Class ?
Class is a blueprint from which objects of same type can be created . Lets take an example of a Bike again . There are thousands of  bikes with the same characteristics i.e having same make and model . They are created from the same prototype / blueprint called class.


 Now we know the basics of an Object and  Class , so lets move on to Object Oriented principles
  • Inheritance 
  • Polymorphism
  • Abstraction 
  • Encapsulation


1. Inheritance :
Inheritance is the capability of one class to acquire the common state and behavior of   another class while adding its own functionality . In Object Oriented programming  , the concept of inheritance provides the idea of re-usability .  This means we can add additional features to a class without modifying it. 

How to achieve Inheritance in Java ?
extends keyword is used to inherit features of a class . 
class Parent {
// Parent has some state and behavior
}

class Child extends Parent {
// Inherit the common state and behavior of Parent class.
// Provide additional functionality.
}


2. Polymorphism :
Polymorphism means ability of one type to take more than one form . This can also be applied in Java where in sub-classes of a class can define their own unique behaviors and yet share some of the common functionalites of the parent class.

Example : 
Lets take an example of Bike . A company can produce different kind of bikes say , a mountain bike , and a roadbike . Now the two different types of bikes inherit the common features of a bike but also define their own separate features.

How to achieve Polymorphism in Java ? 
Overloading  and Overriding are the two ways to achieve Polymorphism.


3. Abstraction :
Abstraction is an essential element of Object Oriented programming . It refers to the act of presenting essential features without including the background details or explanations.

We can say that abstraction is purposeful suppression , hiding of some details of a process or artifact , in order to bring out more clearly other aspects , details or structures. Abstraction can be of control ( abstraction of actions ) or data . 

How to achieve Abstraction in Java ? 
Abstract classes and Interfaces are used to achieve Abstraction in Java.


4. Encapsulation :
Encapsulation is the binding of data and its associated functions in a single unit called Class . Access to the code and data inside the wrapper is tightly controlled through a well defined interface. Encapsulation provides a protective barrier that prevents the access of code and data from outside the class. 

The main benefit of encapsulation is the ability to re-factor the implemented code without breaking the code of others and thus provides flexibility and maintainability .




Wednesday, September 11, 2013

Using HttpClient safely in a Multi-threaded environment.

In my previous post ( Understanding commons HttpClient with example.) , we discussed about the basics of HttpClient with an example . In this post we will learn how we can use HttpClient in a multi-threaded environment.

Why use HttpClient in Multi-threaded environment ?
Now that's a good question . Suppose we need to download multiple files simultaneously .For example  in my project , we have 18 nodes on live environment . Sometimes we need to download log files from all the nodes for doing some log analysis . Now there are various options to do that . 

  • The first approach is that we can download all the files one by one . Do you think that's a good approach ? I think I know your answer :)
  • The second approach is we can use HttpClient with multiple threads to download all the log files concurrently . 


So what is the problem now ?
For execution , each method requires an instance of  HttpConnection.  HttpConnection can only be safely used from a single thread and method at a time . Also connections are finite resources and they should be managed in a proper way . We need  to make sure that they are properly allocated to the methods that require them . 

OK Cool . But how are we going to do that ?
MultiThreadedHttpConnectionManager to the rescue .

Below code snippet shows how we can use it with HttpClient.
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient client = new HttpClient(connectionManager); 


Example : 
In this example , there are three separate threads making a GET request to three different URIs . For convenience , I have taken the URIs in an array .
  • Here instead of creating only HttpClient instance ( as in the previous blog ) , first an instance of MultiThreadedHttpConnectionManager   is created and then its reference is passed to the HttpClient in constructor.
  • Connection is released manually in the finally block .This is because HttpClient doesn't know when a method is not using a connection as method's response is not read directly by the HttpClient. The response is read using method's connection , so a connection can not be released until the response body is read which is after the HttpClient finishes executing the method. 
package httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.StatusLine;
import org.apache.commons.httpclient.methods.GetMethod;

public class MultiThreadedExample {

    public static void main(String[] args) {

        // Creating MultiThreadedHttpConnectionManager
        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

        // Passing it to the HttpClient.
        HttpClient httpClient = new HttpClient(connectionManager);

        // URIs that needs to be GET by threads.
        String[] urisToGet = { "https://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search",
                "https://www.google.com/search?hl=en&q=servicemix&btnG=Google+Search",
                "https://www.google.com/search?hl=en&q=tomcat&btnG=Google+Search", };

        // create a thread for each URI
        for (int i = 0; i < urisToGet.length; i++) {
            GetMethod get = new GetMethod(urisToGet[i]);
            new CreateThread(httpClient, get, "Thread-" + i).start();
        }
    }

    static class CreateThread extends Thread {
        private final HttpClient httpClient;
        private final GetMethod method;

        public CreateThread(HttpClient httpClient, GetMethod method, String name) {
            super(name);
            this.httpClient = httpClient;
            this.method = method;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " - about to get something from "
                        + method.getURI());

                // Execute the method.  
                int statusCode = httpClient.executeMethod(method);

                if (statusCode != HttpStatus.SC_OK) {
                    System.err.println("Method failed: " + method.getStatusLine());
                }

                // Reading the status body.  
                StatusLine statusLine = method.getStatusLine();
                System.out.println(Thread.currentThread().getName() + " " + statusLine);
            } catch (HttpException e) {
                System.err.println("Fatal protocol violation: " + e.getMessage());
            } catch (IOException e) {
                System.err.println("Fatal transport error: " + e.getMessage());
            } finally {
                // Release the connection.  
                method.releaseConnection();
            }
        }

    }

}

Monday, September 9, 2013

Understanding commons HttpClient with example.

What is HttpClient :
HttpClient is a client side  HTTP transport library whose purpose is to transmit and receive HTTP messages. The HTTP (Hyper Text Transfer Protocol ) is the most significant protocol used on the internet today. HttpClient can be used by anyone building HTTP-aware client applications such as Web Browsers , Web services clients or any system that uses HTTP protocol for distributed communication. 


HttpClient is not a browser :
Though it seems that HttpClient is simulating the functionality of a web browser , but that's not true . HttpClient is not a browser. HttpClient will not attempt to cache content, execute javascript embedded in HTML pages, try to guess content type, or reformat request / redirect location URIs.


Get Ready :

Download HttpClient dependencies and put them in the classpath of your application

or if you are a maven user , add dependency in pom :

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0</version>
</dependency>

Steps to create HttpClient :

1. Create an instance of HttpClient.
2. Create an instance of HttpMethod.
3. Tell HttpClient to execute method.
4. Read the respose.
5. Release connection.
6. Deal with the respone. 


Example : This code demonstrates how HttpClient makes a GET request to Google Server and bring back the results.
public class GoogleSearch {
     private static String searchUrl = "https://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search";

     public static void main(String[] args) {

         // Create an instance of HttpClient.
         HttpClient client = new HttpClient();

         // Create a method instance.
         GetMethod method = new GetMethod(searchUrl);

         // Provide custom retry handler.
         method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));

         try {
             // Execute the method.
             int statusCode = client.executeMethod(method);

             if (statusCode != HttpStatus.SC_OK) {
                 System.err.println("Method failed: " + method.getStatusLine());
             }

             // Read the response body.
             String responseBody = method.getResponseBodyAsString();

             // Deal with the response.
             System.out.println(responseBody);

         } catch (HttpException e) {
             System.err.println("Fatal protocol violation: " + e.getMessage());
         } catch (IOException e) {
             System.err.println("Fatal transport error: " + e.getMessage());
         } finally {
             // Release the connection.
             method.releaseConnection();
         }
     }
}

Tuesday, September 3, 2013

Thread interference and Memory consistency errors.

Generally threads communicates by sharing member variables and the Object references members refer to. Well this type of communication is extremely efficient but can cause two kind of problems :

1. Thread Interference 
2. Memory Consistency Errors 

Interference occurs when two operations , running in different threads , but acting on the same data , interleave.

See the Counter class  ( example taken from docs.oracle.com ).

class Counter {
    private int c = 0;

    public void increment() {
        c++;
    }

    public void decrement() {
        c--;
    }

    public int value() {
        return c;
    }

}
Suppose there are two threads Thread-A and Thread-B working on the same counter instance  . Say Thread-A invokes increment() , and at the same time Thread-B invokes decrement() . Thread-A reads the value c and  increment it by 1 . At the same time Thread-B reads the value ( which is 0 because the incremented value is not yet set by Thread-A) , decrements it and set it to -1 . Now Thread-A sets the value to 1 .

Oops what went wrong ? The result of Thread-B is lost. It might be the other case as well or there could be no error at all . This uncertainty makes it difficult to identify bugs .

Memory Consistency Errors occurs when different threads have inconsistent views of the shared data. 

In the above class counter , Lets say there are two threads working on the same counter instance ,  calls the increment method to increase the counter's value by 1 . Here it is no guarantee that the changes made by one thread is visible to the other .

Avoiding Memory Consistency Errors
This can be avoided by establishing a happens-before relationship . This relationship guarantees that memory write by one thread is visible to a read by other thread if the write operation happens-before the read operation. There are various actions that can form a happens-before relationship.

  • Synchronization
  • volatile keyword
  • Thread.start()
  • Thread.join()