<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":1115,"date":"2021-07-20T08:46:02","date_gmt":"2021-07-20T08:46:02","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=1115"},"modified":"2021-09-08T12:08:58","modified_gmt":"2021-09-08T12:08:58","slug":"what-is-deadlock-in-threads-with-example","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/","title":{"rendered":"What is deadlock in threads with Example ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"deadlock-in-threads\" class=\"color-pink\" style=\"text-align: justify;\"><span style=\"font-family: inherit; font-size: 2rem;\">Deadlock in Threads<\/span><\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Deadlock is one of the part of multithreading.<\/li>\n<li><b>synchronized<\/b>\u00a0keyword is used to make the class or method thread-safe which means only one thread can have lock of synchronized method and use it, other threads have to wait till the lock releases and anyone of them acquire that lock.<\/li>\n<li>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.<\/li>\n<li>Since, both threads are waiting for each other to release the lock, the condition is called deadlock.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\">\n<div class=\"col-sm-12\"><\/div>\n<\/div>\n<div class=\"ImageContent\" style=\"text-align: justify;\">\n<div class=\"hddn\"><img decoding=\"async\" class=\"img-responsive center-block aligncenter\" src=\"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png\" alt=\" Deadlock in Threads \" \/><\/div>\n<\/div>\n<p style=\"text-align: justify;\" align=\"center\">Deadlock in Threads<\/p>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"example-of-deadlock-in-thread\" class=\"color-purple\">Example of Deadlock in Thread<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-markdown code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markdown code-embed-code\">public class DeadlockExample {  <br\/>  public static void main(String[] args) {  <br\/>    final String resource1 = &quot;MS Dhoni&quot;;  <br\/>    final String resource2 = &quot;Virat Kholi&quot;;  <br\/>    \/\/ t1 tries to lock resource1 then resource2  <br\/>    Thread t1 = new Thread() {  <br\/>      public void run() {  <br\/>          synchronized (resource1) {  <br\/>           System.out.println(&quot;Thread 1: locked resource 1&quot;);  <br\/>  <br\/>           try { Thread.sleep(100);} catch (Exception e) {}  <br\/>  <br\/>           synchronized (resource2) {  <br\/>            System.out.println(&quot;Thread 1: locked resource 2&quot;);  <br\/>           }  <br\/>         }  <br\/>      }  <br\/>    };  <br\/>  <br\/>    \/\/ t2 tries to lock resource2 then resource1  <br\/>    Thread t2 = new Thread() {  <br\/>      public void run() {  <br\/>        synchronized (resource2) {  <br\/>          System.out.println(&quot;Thread 2: locked resource 2&quot;);  <br\/>  <br\/>          try { Thread.sleep(100);} catch (Exception e) {}  <br\/>  <br\/>          synchronized (resource1) {  <br\/>            System.out.println(&quot;Thread 2: locked resource 1&quot;);  <br\/>          }  <br\/>        }  <br\/>      }  <br\/>    };  <br\/>  <br\/>      <br\/>    t1.start();  <br\/>    t2.start();  <br\/>  }  <br\/>} <\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"Output\">\n<div class=\"hddn\">\n<figure class=\"highlight\" style=\"text-align: justify;\">\n<pre><code class=\"hljs\" data-lang=\"\"><span class=\"nt\"><strong>Thread 1: locked resource 1\r\nThread 2: locked resource 2<\/strong><\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Deadlock is one of the part of multithreading.synchronized keyword is used to&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[7762],"tags":[195,201,491,360,5937,368,203,199,214,209,7817,7818,7819,7820,7821,7822,7823,7800,7801,7824,7802,7803,7825,7826,7827,7828,7829,7830,205,7804,222,484,362,7805,7806,7807,7808,7809,7810,196,212,213,7831,7811,7812,7813,207,366,361,204,7832,7799,367,206,7833,975,200,7814,7815,197,280,364,7834,7835,7816,7836,15905],"class_list":["post-1115","post","type-post","status-publish","format-standard","hentry","category-operating-system","tag-accenture-interview-questions-and-answers","tag-allstate-solutions-pvt-ltd-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-atos-interview-questions-and-answers","tag-aveva-software-interview-questions-and-answers","tag-bmc-software-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-database-deadlock","tag-deadlock","tag-deadlock-avoidance","tag-deadlock-condition","tag-deadlock-definition","tag-deadlock-detection","tag-deadlock-example","tag-deadlock-example-in-c","tag-deadlock-exception-in-java","tag-deadlock-in-dbms","tag-deadlock-in-java","tag-deadlock-in-java-with-realtime-example","tag-deadlock-in-operating-system","tag-deadlock-in-os","tag-deadlock-meaning","tag-deadlock-prevention","tag-deadlock-prevention-in-os","tag-define-deadlock","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-explain-deadlock-situation-in-java","tag-flipkart-interview-questions-and-answers","tag-genpact-interview-questions-and-answers","tag-hcl-technologies-interview-questions-and-answers","tag-how-to-avoid-deadlock-in-c","tag-how-to-avoid-deadlock-in-java","tag-how-to-avoid-deadlock-in-multithreading","tag-how-to-debug-deadlock-in-java","tag-how-to-detect-deadlock-and-fix-it-in-java","tag-how-to-detect-deadlock-in-java","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-java-deadlock-example","tag-java-deadlock-example-and-solution","tag-java-detect-deadlock-thread-dump","tag-jvm-deadlock-detection","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-opentext-technologies-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-oracle-deadlock","tag-prokarma-sof-interview-questions-and-answers","tag-rbs-india-development-centre-pvt-ltd-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-sql-deadlock","tag-tata-consultancy-service-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-thread-deadlock-in-java","tag-to-prevent-deadlock-never-use-synchronized-block-or-method","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-what-is-deadlock","tag-what-is-deadlock-in-os","tag-what-is-deadlock-in-oswrite-a-deadlock-and-resolve-it-in-java","tag-write-a-deadlock-and-resolve-it-in-java","tag-xoxo-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is deadlock in threads with Example ? - Operating System<\/title>\n<meta name=\"description\" content=\"What is deadlock in threads with Example ? - Deadlock in java is one of the part of multithreading.Synchronized keyword is used to make the class\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is deadlock in threads with Example ? - Operating System\" \/>\n<meta property=\"og:description\" content=\"What is deadlock in threads with Example ? - Deadlock in java is one of the part of multithreading.Synchronized keyword is used to make the class\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-20T08:46:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-08T12:08:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/\",\"name\":\"What is deadlock in threads with Example ? - Operating System\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png\",\"datePublished\":\"2021-07-20T08:46:02+00:00\",\"dateModified\":\"2021-09-08T12:08:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"What is deadlock in threads with Example ? - Deadlock in java is one of the part of multithreading.Synchronized keyword is used to make the class\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/#primaryimage\",\"url\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png\",\"contentUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png\"},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is deadlock in threads with Example ? - Operating System","description":"What is deadlock in threads with Example ? - Deadlock in java is one of the part of multithreading.Synchronized keyword is used to make the class","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:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/","og_locale":"en_US","og_type":"article","og_title":"What is deadlock in threads with Example ? - Operating System","og_description":"What is deadlock in threads with Example ? - Deadlock in java is one of the part of multithreading.Synchronized keyword is used to make the class","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/","og_site_name":"Wikitechy","article_published_time":"2021-07-20T08:46:02+00:00","article_modified_time":"2021-09-08T12:08:58+00:00","og_image":[{"url":"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png"}],"author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/","name":"What is deadlock in threads with Example ? - Operating System","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png","datePublished":"2021-07-20T08:46:02+00:00","dateModified":"2021-09-08T12:08:58+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"What is deadlock in threads with Example ? - Deadlock in java is one of the part of multithreading.Synchronized keyword is used to make the class","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example\/#primaryimage","url":"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png","contentUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/operating-system\/what-is-deadlock-in-threads-with-example.png"},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=1115"}],"version-history":[{"count":2,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1115\/revisions"}],"predecessor-version":[{"id":3063,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1115\/revisions\/3063"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=1115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=1115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=1115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}