deadlock in multithreading Java
Deadlock is a condition in which two threads wait for each other to take action which allows them to move further. It's a serious issue because when it happen your program hangs and doesn't do the task it is intended for. In order for deadlock to happen, following four condition must be true :
Mutual Exclusion : At least one resource must be held in a non-shareable mode. Only one process can use the resource at any given instant of time.
Hold and Wait : A process is currently holding at least one resource and requesting additional resources which are being held by other processes.
No Pre-emption : The operating system must not de-allocate resources once they have been allocated; they must be released by the holding process voluntarily.
Circular Wait : A process must be waiting for a resource which is being held by another process, which in turn is waiting for the first process to release the resource.
Easiest way to avoid deadlock is to prevent Circular wait, and this can be done by acquiring locks in a particular order and releasing them in reverse order, so that a thread can only proceed to acquire a lock if it held the other one.
Subscribe to:
Post Comments
(
Atom
)
No Comment to " How do you avoid deadlock in Java? Write Code "