Share this post on:

Let us Discuss How we can Handle Deadlock in Java. When we achieve multitasking in Java Application, we have to face a problem due to Asynchronous of the process.

Deadlock in Java

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 :

  1. Mutual Exclusive
  2. 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:

  1. Synchronized Method
  2. Synchronized Block
  3. 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.

Syntax:

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.

Syntax:

synchronized(reference)

{

}

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:

  1. wait()
  2. notify()
  3. notifyAll()

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.

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *