{"id":801,"date":"2013-02-13T09:11:57","date_gmt":"2013-02-13T09:11:57","guid":{"rendered":"http:\/\/www.softwareeverydayblog.com\/?p=801"},"modified":"2013-02-13T10:16:13","modified_gmt":"2013-02-13T10:16:13","slug":"reentrant-vs-nonreentrant-lock","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=801","title":{"rendered":"Reentrant vs NonReEntrant locks"},"content":{"rendered":"<p>Java 5 has a ReentrantLock implementation of the Lock interface. Meaning if the same thread tries to acquire the lock again, it will allow that. It does have information about which thread is holding the lock. The following code will not cause a deadlock.<\/p>\n<pre lang=\"java\" line=\"1\">\r\nLock l=new ReentrantLock(true);\r\npublic void funcReentrantLock(int level) {\r\n  System.out.println(\"entered funcReentrantLock: \" + Thread.currentThread().getName() + \" at level \" + level);\t\t\r\n  if ( level == 0 )\r\n    return;\r\n\t\t\r\n  l.lock();\r\n  System.out.println(Thread.currentThread().getName() + \" locked at level \" + level);\r\n  funcReentrantLock(level-1);\r\n  l.unlock();\r\n  System.out.println(Thread.currentThread().getName() + \" unlocked at level \" + level);\r\n}\r\n<\/pre>\n<p>Semaphore&#8217;s on the other hand are non-re entrant meaning if the same thread locks and then re-locks it will cause a deadlock. If the same thread tries to acquire the lock again, it wont allow that. It does not have information which thread is holding the lock. The following code will cause a deadlock.<\/p>\n<pre lang=\"java\" line=\"1\">\r\nSemaphore available = new Semaphore(1, true);\r\npublic void funcNonReentrantLock(int level) {\r\n  System.out.println(\"entered funcNonReentrantLock: \" + Thread.currentThread().getName() + \" at level \" + level);\t\t\r\n  try {\r\n    if ( level == 0 )\r\n\treturn;\r\n    available.acquire();\r\n    System.out.println(Thread.currentThread().getName() + \" acquire at level \" + level);\r\n    funcNonReentrantLock(level-1);\r\n    available.release();\r\n    System.out.println(Thread.currentThread().getName() + \" release at level \" + level);\r\n  } catch (InterruptedException e) {\r\n    \/\/ TODO Auto-generated catch block\r\n    e.printStackTrace();\r\n  }\r\n}\r\n<\/pre>\n<p>The <strong>synchronized keyword is a re-entrant lock<\/strong>. Why use one over the other? Well for starters it seems like <a href=\"http:\/\/lycog.com\/concurency\/performance-reentrantlock-synchronized\/\">re-entrant lock performs much better<\/a>. Also, the lock interface provides some other api methods like trylock() which acquires the lock only if it is free at time of invocation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java 5 has a ReentrantLock implementation of the Lock interface. Meaning if the same thread tries to acquire the lock again, it will allow that. It does have information about which thread is holding the lock. The following code will not cause a deadlock. Lock l=new ReentrantLock(true); public void funcReentrantLock(int level) { System.out.println(&#8220;entered funcReentrantLock: &#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=801"}],"version-history":[{"count":11,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":803,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/801\/revisions\/803"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}