同步的好处
- Thesynchronizationismainlyusedto
- Prevent thread interference.
- Prevent consistency problem.
种类
- Therearetwotypesofsynchronization
- Process Synchronization.
- Thread Synchronization.
Thread Synchronization
**Mutual Exclusive: ** helps keep threads from interfering with one another while sharing data.
- Synchronized method.
- Synchronized block.
- Static synchronization.
- Cooperation (Inter-thread communication in java)
Synchronized Block
- Synchronized statements must specify the object that provides the intrinsic lock
- In a synchronized statements , the thread releases the acquired lock when the last statement is executed
- Synchronized block is used to lock an object for any shared resource.
- Scope of synchronized block is smaller than the method.
Deadlock
- The threads t1and t2 are blocked forever, waiting for each other