Synchronization
If multiple threads shares the common area, then data inconsistency will happen. In order to overcome this we need to apply lock. Locking of Method/Block is nothing but synchronization. So that it will allow to execute one thread at a time
Synchronization will be applied to blocks and Methods.
If we apply synchronized to a method then at a time only one thread can run that method. It will not allow other threads to execute till the current thread execution is done.
=>This concept is applicable at method and block level.
=>if we apply synchronization at block level or at method level then only one thread is allowed to execute the block or a method.
=> Advantage -> it resolves the problem of "Data Inconsistency/race condition".
=> Disadvantage->It increase the waiting time for other threads so it affects the performance of the
system.
Note: In java we have 2 levels of lock
a. class level lock => A thread which needs to execute static synchronized block/method needs class level lock. This lock is very unique at the class level.
b. object level lock => A thread which needs to execute synchronized block/method needs object level lock. This lock is very unique at the Object level.
Comments
Post a Comment