<div dir="ltr" style="text-align: justify;" trbidi="on">Deadlock can occur in a situation when a thread is waiting for an object lock, that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread. Since, both threads are waiting for each other to release the lock, the condition is called deadlock.</p>
<p> For example, suppose one thread enters the monitor on object X and another thread enters the monitor on object Y. If the thread in X tries to call any synchronized method on Y, it will block as expected. However, if the thread in Y, in turn, tries to call any synchronized method on X, the thread waits forever, because to access X, it would have to release its own lock on Y so that the first thread could complete.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/deadlock.jpg" /></div>
<p>
<b>Simple Deadlock</b></p>
<p> One of the main problems with threading is Deadlock, two threads are both suspended waiting for the other one to do something. The most common cause of deadlock is two threads both acquiring the same set of (two or more) locks, but in a different order. Consider this code:</p>
<pre class="highlight" name="code">Object A = new Object(); 
Object B = new Object(); 
 
Thread 1: 
 
synchronized(A) 
{ // <;--- preemption 
 synchronized(B) 
 { //... 
 } 
} 
 
Thread 2: 
 
synchronized(B) 
{ synchronized(A) 
 { //... 
 } 
} 
</pre>
<div align='center' id="ads-id"></div>
<p><b>Here&#8217;s the deadlock scenario:</b></p>
<ul>
<li>Thread 1 acquires A, but is then preempted for some reason.</li>
<li>Thread 2 wakes up, acquires B, but can&#8217;t get A because Thread 1 has it, so is suspended.</li>
<li>Thread 1 wakes up, tries to acquire B, but can&#8217;t because Thread 2 has it, so is suspended.</li>
<li>Both threads are now suspended forever. They&#8217;re deadlocked.</li>
</ul>
<p><b>Example of Deadlock in java:</b></p>
<pre class="highlight" name="code">public class DeadLockExample { 
 public static void main(String[] args) { 
 final String resource1 = "dineshonjava.com"; 
 final String resource2 = "tutorial"; 
 // t1 tries to lock resource1 then resource2 
 Thread t1 = new Thread() { 
 public void run() { 
 synchronized (resource1) { 
 System.out.println("Thread 1: locked resource 1"); 
 
 try { 
 Thread.sleep(100); 
 } catch (Exception e) {} 
 
 synchronized (resource2) { 
 System.out.println("Thread 1: locked resource 2"); 
 } 
 } 
 } 
 }; 
 
 // t2 tries to lock resource2 then resource1 
 Thread t2 = new Thread() { 
 public void run() { 
 synchronized (resource2) { 
 System.out.println("Thread 2: locked resource 2"); 
 
 try { 
 Thread.sleep(100); 
 } catch (Exception e) {} 
 
 synchronized (resource1) { 
 System.out.println("Thread 2: locked resource 1"); 
 } 
 } 
 } 
 }; 
 
 
 t1.start(); 
 t2.start(); 
 } 
} 
 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/deadlock.png"/></div>
<p>
Because the program has deadlocked, you need to press <b>CTRL-C</b> to end the program. You can see a full thread and monitor cache dump by pressing <b>CTRL-BREAK</b> on a PC .</p>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/static-synchronization-non-static/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/inter-thread-communication-is-one-of/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/static-synchronization-non-static/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/inter-thread-communication-is-one-of/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '474'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…