– Process synchronization is an essential part of the multiprocessor systems and can be carried out in two methods i.e., either using the synchronized blocks or using the synchronization methods.
– Usually, it is considered better to use the synchronized blocks rather than the methods.
– It has been observed in many of the cases that using synchronization methods can have a negative effect up on the performance of the program.
In this article we discuss about these two ways.
– It is not appropriate to use the method level synchronization where it is require attaining the lock at a fine level.
– Here, we mean to say that suppose the process calls first method and this in turn calls the second method for some verification purpose. In this case a lock is required.
– So, if the synchronization process is put at the method level, then a whole delay might occur because the other processes will put to waiting state.
– The region above the program segment that calls the second method can be synchronized rather than putting the synchronization on the method level.
– If it is done like this all the other threads are free for doing anything before the second method is called rather than having to wait on the method level.
– Plus which method will suit better depends on the code of the program also.
– If the program consists of code that cannot be synchronized, then it would be a good thing to make use of the synchronized block for code since it will let you have a fine control over the things to be synchronized.
– Suppose the same method having so much of code that does not need to be synchronized but still you synchronize it on the method level.
– Because of this the other processes and threads will be put into waiting till the method is finished by the thread that is currently being executed.
– But on the other side if only the required part is only synchronized using the way of synchronized blocks, then the code can be run by the other threads before the execution of the synchronized block without having to wait for the other threads.
– One thing that should be noted is that there has to be no difference between the synchronized block and the synchronized method if the method consists of only the code that is to be synchronized.
– For example, the performance and concurrency level of the two methods must be equal.
Let us take an analogy for making this clearer.
– Consider a house with a main and many rooms plus a kitchen and a bathroom.
– Suppose in that house 5 people live and thus all have equal authority for using these two places.
– If a person wants to use bathroom, he’ll go in and lock it from inside so that no other person is able to use it.
– Even now the other places except bathroom can be used by the remaining persons.
– This is similar to synchronization of a small part of code.
– This is more efficient than the person using the bathroom locking the entire house for the other people.
– Now the other people are left waiting this is indeed very inefficient.
– This whole process is somewhat similar to the synchronization.
– Therefore, from this you can see only the absolute minimum required code must be synchronized.
– This is so because doing so will allow the rest of the code to be executed and utilize the resources even when the some code is being synchronized.
– Going the other way can make your code highly inefficient.
– Today we have a number of languages that provide the support for the multi–threading technology.
– One such language is java.
– It provides multi–threading facility at the language level.
– Most of the support is provided for the data access and the coordination of the activities of the threads.
– Monitor is the mechanism used by java for supporting the synchronization.