{"id":25539704,"date":"2022-08-26T01:52:51","date_gmt":"2022-08-25T20:22:51","guid":{"rendered":"https:\/\/entri.app\/blog\/?p=25539704"},"modified":"2022-08-26T01:52:51","modified_gmt":"2022-08-25T20:22:51","slug":"deadlock-prevention-and-avoidance","status":"publish","type":"post","link":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/","title":{"rendered":"Deadlock Prevention and Avoidance"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e9d91c4b8e3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e9d91c4b8e3\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#What_is_deadlock_in_Java\" >What is deadlock in Java?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#How_to_detect_deadlock_in_Java\" >How to detect deadlock in Java?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#How_to_avoid_deadlock_in_Java\" >How to avoid deadlock in Java?<\/a><\/li><\/ul><\/nav><\/div>\n<p>In a multithreading environment, there are many chances for a Java deadlock situation to occur. A deadlock occurs when two threads wait for each other infinitely and is blocked forever. The threads are blocked since it requires the same lock. For example, consider two threads T1 and T2. T1 acquires lock1 and T2 acquires lock2. Now during execution, T1 wants to acquire lock2 and T2 wants to acquire lock1. Since both the threads are waiting for each other to release the lock, it results in a java deadlock condition. Both the threads acquire the locks by using the\u00a0synchronized\u00a0keyword.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/entri.sng.link\/Bcofz\/uu8c\/w409\" target=\"_blank\" rel=\"noopener\"><strong>To know more about Java, Download Entri App!<\/strong><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_deadlock_in_Java\"><\/span><strong>What is deadlock in Java?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In the thread, each object has a lock. To acquire a lock,\u00a0Java\u00a0provides synchronization to lock a method or code block. It allows that at a time only one thread can access that method.<\/p>\n<p>Nevertheless, if a thread wants to execute a synchronized method it first tries to acquire a lock. It is possible that another thread has already acquired that lock then the thread (that wants to acquire the lock) will have to wait until the previous thread does not release the lock.<\/p>\n<p>Let&#8217;s understand it through an example.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>Suppose, there are two threads A and B. The thread A and B acquired the lock of Object-A and Object-B, respectively. Assume that thread A executing method A and wants to acquire the lock on Object-B, while thread B is already acquired a lock on Object-B.<\/p>\n<p>On the other hand, thread B also tries to acquire a lock on Object-A, while thread A is acquired a lock on Object-A. In such a situation both threads will not complete their execution and wait for releasing the lock. The situation is known as,\u00a0<strong>deadlock<\/strong>.<\/p>\n<p><strong>public<\/strong><strong>void<\/strong>\u00a0methodA()<\/p>\n<p>{<\/p>\n<p>\/\/&#8230;<\/p>\n<p><strong>synchronized<\/strong>(lockA)<\/p>\n<p>{<\/p>\n<p>\/\/&#8230;<\/p>\n<p><strong>synchronized<\/strong>(lockB)<\/p>\n<p>{<\/p>\n<p>\/\/&#8230;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>public<\/strong><strong>void<\/strong>\u00a0methodB()<\/p>\n<p>{<\/p>\n<p>\/\/&#8230;<\/p>\n<p><strong>synchronized<\/strong>(lockB)<\/p>\n<p>{<\/p>\n<p>\/\/&#8230;<\/p>\n<p><strong>synchronized<\/strong>(lockA)<\/p>\n<p>{<\/p>\n<p>\/\/&#8230;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/entri.sng.link\/Bcofz\/uu8c\/w409\" target=\"_blank\" rel=\"noopener\"><strong>To know more about Java, Download Entri App!<\/strong><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_detect_deadlock_in_Java\"><\/span>How to detect deadlock in Java?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are following ways to detect a deadlock:<\/p>\n<ul>\n<li>First, we look and understand the code if we found nested synchronized block or trying to get a lock on a different object or calling a synchronized method from other synchronized method, these reason leads to a deadlock situation.<\/li>\n<li>Another way to detect deadlock is to use the\u00a0<strong>io<\/strong> It allows us to upload a thread dump and analyze it.<\/li>\n<li>We can also use\u00a0<strong>jConsole<\/strong>or\u00a0<strong>VisualVM<\/strong>\u00a0to detect deadlock. It shows us which threads are getting locked and on which object.<\/li>\n<\/ul>\n<p>Consider a situation when two bank accounts trying to transfer money to each other at the same time. It means that one account is debited and another account is credited, and vice-versa. Let&#8217;s implement the situation in a Java program.<\/p>\n<p>In the following example, we can easily detect a deadlock. Because two threads are sending money to each other at the same time that leads to a deadlock situation.<\/p>\n<p><strong>Account.java<\/strong><\/p>\n<p><strong>class<\/strong>Account<\/p>\n<p>{<\/p>\n<p><strong>double<\/strong>balance;<\/p>\n<p><strong>void<\/strong>\u00a0deposit(<strong>double<\/strong>\u00a0amount)<\/p>\n<p>{<\/p>\n<p>balance\u00a0+=\u00a0amount;<\/p>\n<p>}<\/p>\n<p><strong>void<\/strong>\u00a0withdraw(<strong>double<\/strong>\u00a0amount)<\/p>\n<p>{<\/p>\n<p>balance\u00a0-=\u00a0amount;<\/p>\n<p>}<\/p>\n<p><strong>void<\/strong>\u00a0transfer(Account\u00a0from,\u00a0Account\u00a0to,\u00a0<strong>double<\/strong>\u00a0amount){<\/p>\n<p>sync(from);<\/p>\n<p>sync(to);<\/p>\n<p>from.withdraw(amount);<\/p>\n<p>deposit(amount);<\/p>\n<p>release(to);<\/p>\n<p>release(from);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>In the above example, there are two threads that are attempting to transfer money to each other at the same time. It creates a deadlock situation because the threads try to acquire the lock in the reverse order.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_avoid_deadlock_in_Java\"><\/span>How to avoid deadlock in Java?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Although it is not possible to avoid deadlock condition but we can avoid it by using the following ways:<\/p>\n<ul>\n<li><strong>Avoid Unnecessary Locks:<\/strong>We should use locks only for those members on which it is required. Unnecessary use of locks leads to a deadlock situation. We recommend you to use a\u00a0<strong>lock-free<\/strong>\u00a0data structure. If possible, keep your code free form locks. For example, instead of using synchronized\u00a0<strong>ArrayList<\/strong>\u00a0use the\u00a0<strong>ConcurrentLinkedQueue<\/strong>.<\/li>\n<li><strong>Avoid Nested Locks:<\/strong>Another way to avoid deadlock is to avoid giving a lock to multiple threads if we have already provided a lock to one thread. Since we must avoid allocating a lock to multiple threads.<\/li>\n<li><strong>Using Thread.join() Method:<\/strong>You can get a deadlock if two threads are waiting for each other to finish indefinitely using thread join. If your thread has to wait for another thread to finish, it&#8217;s always best to use join with the maximum time you want to wait for the thread to finish.<\/li>\n<li><strong>Use Lock Ordering:<\/strong>Always assign a numeric value to each lock. Before acquiring the lock with a higher numeric value, acquire the locks with a lower numeric value.<\/li>\n<li><strong>Lock Time-out:<\/strong>We can also specify the time for a thread to acquire a lock. If a thread does not acquire a lock, the thread must wait for a specific time before retrying to acquire a lock.<\/li>\n<\/ul>\n<p>Let&#8217;s understand it through an example.<\/p>\n<p><strong>AvoidDeadlockExamplet.java<\/strong><\/p>\n<p><strong>public<\/strong><strong>class<\/strong>\u00a0AvoidDeadlockExample<\/p>\n<p>{<\/p>\n<p><strong>public<\/strong><strong>static<\/strong>\u00a0<strong>void<\/strong>\u00a0main(String[]\u00a0args)\u00a0<strong>throws<\/strong>\u00a0InterruptedException<\/p>\n<p>{<\/p>\n<p>\/\/creating\u00a0object\u00a0of\u00a0the\u00a0Object\u00a0class<\/p>\n<p>Object\u00a0object1\u00a0=\u00a0<strong>new<\/strong>\u00a0Object();<\/p>\n<p>Object\u00a0object2\u00a0=\u00a0<strong>new<\/strong>Object();<\/p>\n<p>Object\u00a0object3\u00a0=\u00a0<strong>new<\/strong>\u00a0Object();<\/p>\n<p>\/\/creating\u00a0constructor\u00a0of\u00a0the\u00a0Thread\u00a0class\u00a0and\u00a0passing\u00a0SynchroniseThread\u00a0object\u00a0as\u00a0a\u00a0parameter<\/p>\n<p>Thread\u00a0thread1\u00a0=\u00a0<strong>new<\/strong>\u00a0Thread(<strong>new<\/strong>\u00a0SynchroniseThread(object1,\u00a0object2),\u00a0&#8220;thread1&#8221;);<\/p>\n<p>Thread\u00a0thread2\u00a0=\u00a0<strong>new<\/strong>Thread(<strong>new<\/strong>\u00a0SynchroniseThread(object2,\u00a0object3),\u00a0&#8220;thread2&#8221;);<\/p>\n<p>\/\/executing\u00a0thread1<\/p>\n<p>start();<\/p>\n<p>\/\/the\u00a0sleep()\u00a0method\u00a0suspends\u00a0the\u00a0execution\u00a0of\u00a0the\u00a0current\u00a0thread\u00a0(thread1)\u00a0for\u00a0the\u00a0specific\u00a0period<\/p>\n<p>sleep(2000);<\/p>\n<p>\/\/executing\u00a0thread2<\/p>\n<p>start();<\/p>\n<p>\/\/suspends\u00a0the\u00a0execution\u00a0of\u00a0the\u00a0current\u00a0thread\u00a0(thread2)\u00a0for\u00a0the\u00a0specific\u00a0period<\/p>\n<p>sleep(2000);<\/p>\n<p>}}<\/p>\n<p><strong>class<\/strong>\u00a0SynchroniseThread\u00a0<strong>implements<\/strong>\u00a0Runnable<\/p>\n<p>{<\/p>\n<p><strong>private<\/strong>\u00a0Object\u00a0object1;<\/p>\n<p><strong>private<\/strong>Object\u00a0object2;<\/p>\n<p><strong>public<\/strong>\u00a0SynchroniseThread(Object\u00a0o1,\u00a0Object\u00a0o2)<\/p>\n<p>{<\/p>\n<p><strong>this<\/strong>.object1=o1;<\/p>\n<p><strong>this<\/strong>.object2=o2;<\/p>\n<p>}<\/p>\n<p>\/\/overriding\u00a0run()\u00a0method\u00a0of\u00a0the\u00a0Thread\u00a0class<\/p>\n<p>@Override<\/p>\n<p>\/\/it\u00a0allows\u00a0two\u00a0threads\u00a0are\u00a0running\u00a0concurrently<\/p>\n<p><strong>public<\/strong>\u00a0<strong>void<\/strong>\u00a0run()<\/p>\n<p>{<\/p>\n<p>\/\/getteing\u00a0the\u00a0current\u00a0thread\u00a0name<\/p>\n<p>String\u00a0name\u00a0=\u00a0Thread.currentThread().getName();<\/p>\n<p>System.out.println(name\u00a0+\u00a0&#8221;\u00a0acquire\u00a0lock\u00a0on\u00a0&#8221;\u00a0+\u00a0object1);<\/p>\n<p>\/\/Synchronized()\u00a0method\u00a0is\u00a0used\u00a0to\u00a0lock\u00a0an\u00a0object\u00a0for\u00a0any\u00a0shared\u00a0resource.\u00a0When\u00a0a\u00a0thread\u00a0invokes\u00a0the\u00a0synchronized()\u00a0method,<\/p>\n<p>\/\/it\u00a0automatically\u00a0acquires\u00a0the\u00a0lock\u00a0for\u00a0that\u00a0object\u00a0and\u00a0releases\u00a0it\u00a0when\u00a0the\u00a0thread\u00a0completes\u00a0its\u00a0task.<\/p>\n<p><strong>synchronized<\/strong>(object1)<\/p>\n<p>{<\/p>\n<p>out.println(name\u00a0+\u00a0&#8221;\u00a0acquired\u00a0lock\u00a0on\u00a0&#8220;+\u00a0object1);<\/p>\n<p>\/\/calling\u00a0work()\u00a0method<\/p>\n<p>work();<\/p>\n<p>}<\/p>\n<p>out.println(name\u00a0+\u00a0&#8221;\u00a0released\u00a0lock\u00a0of\u00a0&#8220;+\u00a0object1);<\/p>\n<p>System.out.println(name\u00a0+\u00a0&#8221;\u00a0acquire\u00a0lock\u00a0on\u00a0&#8221;\u00a0+\u00a0object2);<\/p>\n<p><strong>synchronized<\/strong>(object2)<\/p>\n<p>{<\/p>\n<p>out.println(name\u00a0+\u00a0&#8221;\u00a0acquire\u00a0lock\u00a0on\u00a0&#8220;+\u00a0object2);<\/p>\n<p>work();<\/p>\n<p>}<\/p>\n<p>System.out.println(name\u00a0+\u00a0&#8221;\u00a0released\u00a0lock\u00a0of\u00a0&#8221;\u00a0+\u00a0object2);<\/p>\n<p>out.println(name\u00a0+\u00a0&#8221;\u00a0execution\u00a0is\u00a0completed.&#8221;);<\/p>\n<p>}<\/p>\n<p><strong>private<\/strong><strong>void<\/strong>\u00a0work()<\/p>\n<p>{<\/p>\n<p><strong>try<\/strong><\/p>\n<p>{<\/p>\n<p>\/\/the\u00a0sleep()\u00a0method\u00a0suspends\u00a0the\u00a0execution\u00a0of\u00a0the\u00a0current\u00a0thread\u00a0for\u00a05\u00a0seconds<\/p>\n<p>Thread.sleep(5000);<\/p>\n<p>}<\/p>\n<p><strong>catch<\/strong>\u00a0(InterruptedException\u00a0e)<\/p>\n<p>{<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>We recommend you that if you are dealing with multiple locks, always acquire the locks with lower numerical value before acquiring the locks with a higher numeric value.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/entri.sng.link\/Bcofz\/uu8c\/w409\" target=\"_blank\" rel=\"noopener\"><strong>To know more about Java, Download Entri App!<\/strong><\/a><\/p>\n<p><strong>Why is it important to choose Entri?<\/strong><\/p>\n<ul>\n<li>Excellent online platform for all the Competitive Exams.<\/li>\n<li>Provides updated materials created by the Entri Experts.<\/li>\n<li>Entri provides a best platform with full- length mock tests including previous year question papers.<\/li>\n<li>You can download the app for free and join the required classes.<\/li>\n<li>Entri wishes you all the best for your examinations and future endeavours.<\/li>\n<\/ul>\n<p><strong>\u201cYOU DON\u2019T HAVE TO BE GREAT TO START, BUT YOU HAVE TO START TO BE GREAT.\u201d<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a multithreading environment, there are many chances for a Java deadlock situation to occur. A deadlock occurs when two threads wait for each other infinitely and is blocked forever. The threads are blocked since it requires the same lock. For example, consider two threads T1 and T2. T1 acquires lock1 and T2 acquires lock2. [&hellip;]<\/p>\n","protected":false},"author":55,"featured_media":25539706,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[802,558,1882],"tags":[],"class_list":["post-25539704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-general-knowledge","category-java-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deadlock Prevention and Avoidance - Entri Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deadlock Prevention and Avoidance - Entri Blog\" \/>\n<meta property=\"og:description\" content=\"In a multithreading environment, there are many chances for a Java deadlock situation to occur. A deadlock occurs when two threads wait for each other infinitely and is blocked forever. The threads are blocked since it requires the same lock. For example, consider two threads T1 and T2. T1 acquires lock1 and T2 acquires lock2. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/\" \/>\n<meta property=\"og:site_name\" content=\"Entri Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/entri.me\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-25T20:22:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png\" \/>\n\t<meta property=\"og:image:width\" content=\"820\" \/>\n\t<meta property=\"og:image:height\" content=\"615\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ayesha Surayya\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@entri_app\" \/>\n<meta name=\"twitter:site\" content=\"@entri_app\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ayesha Surayya\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/\"},\"author\":{\"name\":\"Ayesha Surayya\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097\"},\"headline\":\"Deadlock Prevention and Avoidance\",\"datePublished\":\"2022-08-25T20:22:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/\"},\"wordCount\":1186,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png\",\"articleSection\":[\"Articles\",\"General Knowledge\",\"Java Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/\",\"url\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/\",\"name\":\"Deadlock Prevention and Avoidance - Entri Blog\",\"isPartOf\":{\"@id\":\"https:\/\/entri.app\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png\",\"datePublished\":\"2022-08-25T20:22:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png\",\"width\":820,\"height\":615,\"caption\":\"Deadlock Prevention and Avoidance\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/entri.app\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Entri Skilling\",\"item\":\"https:\/\/entri.app\/blog\/category\/entri-skilling\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Java Programming\",\"item\":\"https:\/\/entri.app\/blog\/category\/entri-skilling\/java-programming\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Deadlock Prevention and Avoidance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/entri.app\/blog\/#website\",\"url\":\"https:\/\/entri.app\/blog\/\",\"name\":\"Entri Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/entri.app\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/entri.app\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/entri.app\/blog\/#organization\",\"name\":\"Entri App\",\"url\":\"https:\/\/entri.app\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"contentUrl\":\"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png\",\"width\":989,\"height\":446,\"caption\":\"Entri App\"},\"image\":{\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/entri.me\/\",\"https:\/\/x.com\/entri_app\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097\",\"name\":\"Ayesha Surayya\",\"url\":\"https:\/\/entri.app\/blog\/author\/ayesha-surayya\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Deadlock Prevention and Avoidance - Entri Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/","og_locale":"en_US","og_type":"article","og_title":"Deadlock Prevention and Avoidance - Entri Blog","og_description":"In a multithreading environment, there are many chances for a Java deadlock situation to occur. A deadlock occurs when two threads wait for each other infinitely and is blocked forever. The threads are blocked since it requires the same lock. For example, consider two threads T1 and T2. T1 acquires lock1 and T2 acquires lock2. [&hellip;]","og_url":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/","og_site_name":"Entri Blog","article_publisher":"https:\/\/www.facebook.com\/entri.me\/","article_published_time":"2022-08-25T20:22:51+00:00","og_image":[{"width":820,"height":615,"url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png","type":"image\/png"}],"author":"Ayesha Surayya","twitter_card":"summary_large_image","twitter_creator":"@entri_app","twitter_site":"@entri_app","twitter_misc":{"Written by":"Ayesha Surayya","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#article","isPartOf":{"@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/"},"author":{"name":"Ayesha Surayya","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097"},"headline":"Deadlock Prevention and Avoidance","datePublished":"2022-08-25T20:22:51+00:00","mainEntityOfPage":{"@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/"},"wordCount":1186,"commentCount":0,"publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"image":{"@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png","articleSection":["Articles","General Knowledge","Java Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/","url":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/","name":"Deadlock Prevention and Avoidance - Entri Blog","isPartOf":{"@id":"https:\/\/entri.app\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage"},"image":{"@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage"},"thumbnailUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png","datePublished":"2022-08-25T20:22:51+00:00","breadcrumb":{"@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#primaryimage","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2022\/08\/Deadlock-Prevention-and-Avoidance.png","width":820,"height":615,"caption":"Deadlock Prevention and Avoidance"},{"@type":"BreadcrumbList","@id":"https:\/\/entri.app\/blog\/deadlock-prevention-and-avoidance\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/entri.app\/blog\/"},{"@type":"ListItem","position":2,"name":"Entri Skilling","item":"https:\/\/entri.app\/blog\/category\/entri-skilling\/"},{"@type":"ListItem","position":3,"name":"Java Programming","item":"https:\/\/entri.app\/blog\/category\/entri-skilling\/java-programming\/"},{"@type":"ListItem","position":4,"name":"Deadlock Prevention and Avoidance"}]},{"@type":"WebSite","@id":"https:\/\/entri.app\/blog\/#website","url":"https:\/\/entri.app\/blog\/","name":"Entri Blog","description":"","publisher":{"@id":"https:\/\/entri.app\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/entri.app\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/entri.app\/blog\/#organization","name":"Entri App","url":"https:\/\/entri.app\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","contentUrl":"https:\/\/entri.app\/blog\/wp-content\/uploads\/2019\/10\/Entri-Logo-1.png","width":989,"height":446,"caption":"Entri App"},"image":{"@id":"https:\/\/entri.app\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/entri.me\/","https:\/\/x.com\/entri_app"]},{"@type":"Person","@id":"https:\/\/entri.app\/blog\/#\/schema\/person\/568cc9d6e77fd5d01033b61c88343097","name":"Ayesha Surayya","url":"https:\/\/entri.app\/blog\/author\/ayesha-surayya\/"}]}},"_links":{"self":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25539704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/users\/55"}],"replies":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/comments?post=25539704"}],"version-history":[{"count":3,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25539704\/revisions"}],"predecessor-version":[{"id":25539708,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/posts\/25539704\/revisions\/25539708"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media\/25539706"}],"wp:attachment":[{"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/media?parent=25539704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/categories?post=25539704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/entri.app\/blog\/wp-json\/wp\/v2\/tags?post=25539704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}