<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">Deadlock can occur in a situation when a thread is waiting for an object lock&comma; that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread&period; Since&comma; both threads are waiting for each other to release the lock&comma; the condition is called deadlock&period;<&sol;p>&NewLine;<p> For example&comma; suppose one thread enters the monitor on object X and another thread enters the monitor on object Y&period; If the thread in X tries to call any synchronized method on Y&comma; it will block as expected&period; However&comma; if the thread in Y&comma; in turn&comma; tries to call any synchronized method on X&comma; the thread waits forever&comma; because to access X&comma; it would have to release its own lock on Y so that the first thread could complete&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;deadlock&period;jpg" &sol;><&sol;div>&NewLine;<p>&NewLine;<b>Simple Deadlock<&sol;b><&sol;p>&NewLine;<p> One of the main problems with threading is Deadlock&comma; two threads are both suspended waiting for the other one to do something&period; The most common cause of deadlock is two threads both acquiring the same set of &lpar;two or more&rpar; locks&comma; but in a different order&period; Consider this code&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">Object A &equals; new Object&lpar;&rpar;&semi; &NewLine;Object B &equals; new Object&lpar;&rpar;&semi; &NewLine; &NewLine;Thread 1&colon; &NewLine; &NewLine;synchronized&lpar;A&rpar; &NewLine;&lbrace; &sol;&sol; &lt&semi;--- preemption &NewLine; synchronized&lpar;B&rpar; &NewLine; &lbrace; &sol;&sol;&period;&period;&period; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;Thread 2&colon; &NewLine; &NewLine;synchronized&lpar;B&rpar; &NewLine;&lbrace; synchronized&lpar;A&rpar; &NewLine; &lbrace; &sol;&sol;&period;&period;&period; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>Here&&num;8217&semi;s the deadlock scenario&colon;<&sol;b><&sol;p>&NewLine;<ul>&NewLine;<li>Thread 1 acquires A&comma; but is then preempted for some reason&period;<&sol;li>&NewLine;<li>Thread 2 wakes up&comma; acquires B&comma; but can&&num;8217&semi;t get A because Thread 1 has it&comma; so is suspended&period;<&sol;li>&NewLine;<li>Thread 1 wakes up&comma; tries to acquire B&comma; but can&&num;8217&semi;t because Thread 2 has it&comma; so is suspended&period;<&sol;li>&NewLine;<li>Both threads are now suspended forever&period; They&&num;8217&semi;re deadlocked&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>Example of Deadlock in java&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public class DeadLockExample &lbrace; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; final String resource1 &equals; "dineshonjava&period;com"&semi; &NewLine; final String resource2 &equals; "tutorial"&semi; &NewLine; &sol;&sol; t1 tries to lock resource1 then resource2 &NewLine; Thread t1 &equals; new Thread&lpar;&rpar; &lbrace; &NewLine; public void run&lpar;&rpar; &lbrace; &NewLine; synchronized &lpar;resource1&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Thread 1&colon; locked resource 1"&rpar;&semi; &NewLine; &NewLine; try &lbrace; &NewLine; Thread&period;sleep&lpar;100&rpar;&semi; &NewLine; &rcub; catch &lpar;Exception e&rpar; &lbrace;&rcub; &NewLine; &NewLine; synchronized &lpar;resource2&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Thread 1&colon; locked resource 2"&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub;&semi; &NewLine; &NewLine; &sol;&sol; t2 tries to lock resource2 then resource1 &NewLine; Thread t2 &equals; new Thread&lpar;&rpar; &lbrace; &NewLine; public void run&lpar;&rpar; &lbrace; &NewLine; synchronized &lpar;resource2&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Thread 2&colon; locked resource 2"&rpar;&semi; &NewLine; &NewLine; try &lbrace; &NewLine; Thread&period;sleep&lpar;100&rpar;&semi; &NewLine; &rcub; catch &lpar;Exception e&rpar; &lbrace;&rcub; &NewLine; &NewLine; synchronized &lpar;resource1&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Thread 2&colon; locked resource 1"&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub;&semi; &NewLine; &NewLine; &NewLine; t1&period;start&lpar;&rpar;&semi; &NewLine; t2&period;start&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;deadlock&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;Because the program has deadlocked&comma; you need to press <b>CTRL-C<&sol;b> to end the program&period; You can see a full thread and monitor cache dump by pressing <b>CTRL-BREAK<&sol;b> on a PC &period;<&sol;p>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;static-synchronization-non-static&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;inter-thread-communication-is-one-of&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;static-synchronization-non-static&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;inter-thread-communication-is-one-of&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '474'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago