Asynchronous : When a Operating system or Program then it share a common source by the multiple user then the process become deadlock. Deadlock is a situation where a process of program goes into infinite loop. The reason behind this is Asynchronous.
Deadlock describe a situation where two or more threads are block forever, waiting for each other deadlock occur when multiple thread need the same lock/monitor but obtain them in different order. In Java multithreading program may suffer from the deadlock condition because the synchronous keyword.
For overcome Asynchronous to Synchronous provide two Approach :
- Mutual Exclusive
- Inter-Thread Communication
Mutual Exclusive: In Mutual Exclusive one or more than one resource non-shareable hold and wait. A process is holding at least one resource and waiting for resource.
Lock: Lock is a method or process where a thread synchronization mechanism can be implements by default every method of class have a lock.
There are three ways to achieve lock of a specific method of a class:
- Synchronized Method
- Synchronized Block
- static Synchronized method
Synchronized Method : It is a simple method associate with synchronized keyword. It is used to achieve the lock for Individual thread or client.
modifier synchronized return type methodName(arguments if any)
Synchronized Block : It is used to achieve lock of a specific statement or part within a method. It scope is less that synchronized method.
Inter Thread Communication : It is a also called as Co-operation in this process one thread can be communicate with each other by following three methods:
wait(): It is used to suspend the current thread until notify or notifyAll method is not invoke.
Notify() / NotifyAll() : It is used for active the thread which are suspended by wait method.