synchronization in java is the capability to control the access of multiple threads to any shared resource.
java synchronization is better option where we want to allow only one thread to access the shared resource.
the synchronization is mainly used to:
- to prevent thread interference
- to prevent consistency problem.
there are two types of synchroniization :
- process synchronization
- thread synchronization
thread synchronization:there are two types of thread synchronization mutual exclusive and inter thread communication.
- mutual exclusive
- synchronized method
- synchronized block
- static synchronization
2 .cooperation
mutual exclusive
mutual exclusive helps keep threads from interfering with one another while sharing data,this can be done by three ways in java:
- by synchronized method
- by synchronized block
- by static synchronization
lock in java:
synchronization is built around an internal entity known as the lock or monitor .every object has an lock associated with it.by convention,a thread that needs consistent access to an object's lock before accessing them,and then release the lock when it's done with them.