Data access by Client Pull or by Server Push

This is a design consideration between client - server interaction, i.e. whether client should call the server and pull the data from the server as and when required. Or the server should push the data to the client whenever there is some new data for the client.

Out of these two approaches, the first approach i.e. Client Pull is the most common approach where client opens a connection with the server (using any client/server protocol) and ask the server for the data. Here client pull the required data from the server as and when required.

Second approach is, where the server keep track of all its clients. Whenever server find any data which should be sent to the client, it pushes the data to the client. In this approach, there should be some provision using which server can interact to the client, like client should have some server socket open with it or client request should be hold on server for pending response for a long time. Now server keep track of all its clients. If we are working with server socket at the client, then it should be opened for all the time, so that server can connect to it anytime for pushing the data to client.

Sometime we find a need where server need to update the client for any latest data, like share trading software or weather information software. Here server keep getting updated data from multiple sources and now same data should be updated to all client terminals of the server. Now above mentioned both approaches can be used here as mentioned below:

Simplifying the Locking Mechanism in Threads

synchronized(lockObject)
{
    // do the job
}

This is one of the ways to acquire a lock on an object and then do the work in a code block with exclusive rights. Synchronization is already explained with another blog 'Mystery of Synchronized Keyword'. 

Here the question is that why we take the lock on an Object, not on current thread. Answer is fairly simple. Whenever you want to secure the admission to any premises, you never apply a lock on yourselves but you will put a lock on some other object, like on door. So the thread is the 'actor' (you) who want to perform some work in a code block (premises). Here the thread want a exclusive access on a code block where it want to work. Now procedure to have exclusive access is to have a lock for code block, so that only the current thread can have access rights to it. Here thread needs any object (door), like lockObject in above example, to have lock on the code block. 

Further we need to understand that how thread get lock using an object. To understand the story under the hood, consider that every Object is having a state within it which shows that whether any thread has lock on this object or not. Whenever any thread request the lock on any object, object checks whether its lock property is already set or not. If not, it set its property for current thread otherwise it refuse to give the lock, ask the current thread to wait till previous thread release the lock and put the thread in queue. Once the previous thread release the lock, and hence unset the property, Object picks the next thread from queue and set the lock property for it. As soon as thread get the object property set for it, it get the rights to enter in code block and to execute it.

Another example is like more than one painters are painting an house. Now painting of one room needs that only one painter works in it at a time for good output. Like first one painter should apply the putty on the wall, then the next one should apply the primer and paint and so on. To ensure that everyone follow this, painter decide to lock the door of the room whenever anyone enter inside for the work. So as soon as putty man enters the room, he locks the door from inside. When other come for the work, they find the door locked and hence wait for their turn. As soon as, first man finishes his work, he opens the door and the next painter waiting in the queue gets the access to room. 

Runtime environment of Java ensure all of this processing i.e. it puts the current thread to wait state till it gets access on current object lock property and put it back to execution after getting the property set for it. 


Mystery of Synchronized Keyword

'Synchronized' keyword is used to ensure that only one thread access a code block or a method at a time. It is very important keyword in multi-threaded programming. In multi-threaded programming, multiple threads work on the same code blocks simultaneously. But there can be some code blocks which manipulate shared data. So if multiple threads executes the same code and try to manipulate the same data, it can results in inconsistent result. For example:

public class TestSynchronized {

private int i;

// suppose param value is 5
public void testMethod( int param)
{
    // thread update i to 5
    // simultaneously other thread enters the block and update i to 10 before first thread would be able to            
    // execute next statement
    i = i + param;

    // result is expected to be 25
    // but result comes to 50, as second thread updates i to 10 before first thread executes the multiple
    //statement

    int result = i*5;

    System.println.out("thread[" + Thread.currentThread + "] result[" + result  + "]");
    i = 0;
}

}