tag:blogger.com,1999:blog-8712770457197348465.post6604050668377450608..comments2024-03-18T22:43:46.707-07:00Comments on Javarevisited: How to avoid deadlock in Java? Example Tutorial and Tipsjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger42125tag:blogger.com,1999:blog-8712770457197348465.post-52980321602033669292022-04-10T19:39:24.275-07:002022-04-10T19:39:24.275-07:00What crap is this? Remove this post right now. The...What crap is this? Remove this post right now. The solution on how to avoid deadlock is not to have it. DUH!!!<br />Solution for not being a father is not have kids!! Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28394594053916063492019-11-19T21:42:42.970-08:002019-11-19T21:42:42.970-08:00Hey javin, I know this might be a silly question b...Hey javin, I know this might be a silly question but in ur code where are you exactly releasing locks and as u mentioned that locks should be released in opposite order y should it be so, I'm a beginner so I do not know much about locks old help me out. Nishitahttps://www.blogger.com/profile/00598353126304102505noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13743576638657211572019-08-13T17:46:05.131-07:002019-08-13T17:46:05.131-07:00Hello Anshul, methods are not synchronized - they ...Hello Anshul, methods are not synchronized - they are using Synchronized block with explicit locks.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10764173236408563722019-08-13T06:50:35.717-07:002019-08-13T06:50:35.717-07:00I cudnt understand the example given, both method1...I cudnt understand the example given, both method1 and method2 are synchronized, so if any thread aquires 1 lock on Integer Class, another one will wait until it releases the lock to proceed. So where is the deadlock occurring?Anshulhttps://www.blogger.com/profile/17565735068387520176noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21445035752506147702019-08-07T05:23:40.769-07:002019-08-07T05:23:40.769-07:00public void method1() {
synchronized (Inte... public void method1() {<br /> synchronized (Integer.class) {<br /> System.out.println("Aquired lock on Integer.class object");<br /><br /> synchronized (String.class) {<br /> System.out.println("Aquired lock on String.class object");<br /> }<br /> }<br /> }<br /><br /> public void method2() {<br /> Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39143450866947210482018-08-19T19:53:33.696-07:002018-08-19T19:53:33.696-07:00Are there still anyone who uses synchronized?Are there still anyone who uses synchronized?Anonymoushttps://www.blogger.com/profile/07843298473658926656noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76041590588849663982016-06-21T04:51:06.607-07:002016-06-21T04:51:06.607-07:00@Anonymous, thanks for sharing such a nice tool, d...@Anonymous, thanks for sharing such a nice tool, did you built it?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25895973993948836612016-06-20T21:54:16.955-07:002016-06-20T21:54:16.955-07:00http://fastthread.io/ is a universal thread dump a...<a href="http://fastthread.io/" rel="nofollow">http://fastthread.io/</a> is a universal thread dump analyser. It parses complex thread dumps and presents with you with insightful metrics and beautiful graphs. I will recommend this tool to all my java team members.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32177338699782902212015-07-13T08:48:54.921-07:002015-07-13T08:48:54.921-07:00@Anonymous, if situation demand that you need mult...@Anonymous, if situation demand that you need multiple locks to access a resource then you have to. For example, how do you access something which is behind two doors? Accessing them in proper order helps to break the deadlock. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67132630996052547002015-07-13T00:58:05.561-07:002015-07-13T00:58:05.561-07:00I know what is dead Lock and how it forms in prog...I know what is dead Lock and how it forms in program but in above explanation and example what you gave is i dint get you used synchronized lock which is nested instead of nested if we use non-nested loop than we can avoid deadlocksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43627939725391128782015-05-01T12:37:36.477-07:002015-05-01T12:37:36.477-07:00we can avoid deadlock using lock time out .
can yo...we can avoid deadlock using lock time out .<br />can you using java code example.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86971109286660055952015-04-27T00:28:00.787-07:002015-04-27T00:28:00.787-07:00'Now there would not be any deadlock because b...'Now there would not be any deadlock because both method is accessing lock on Integer and String object in same order' What if I really need to acces the locks in the reverse order. How do I deal with that?saytidahttps://www.blogger.com/profile/16904955270370642982noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78803872426420491512015-01-19T09:38:58.279-08:002015-01-19T09:38:58.279-08:00clear & concise explanation. thanksclear & concise explanation. thanks Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59087800857422709032014-11-28T01:42:32.220-08:002014-11-28T01:42:32.220-08:00here inner class must and should start with static...here inner class must and should start with static keyword .otherwise its not working .its give comple time errorAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20914499799125164322014-08-16T23:11:13.087-07:002014-08-16T23:11:13.087-07:00Hello Bhavya Shah, you are absolutely correct, usi...Hello Bhavya Shah, you are absolutely correct, using String.class or Integer.class or just using class literal from any other class is NOT a good idea. In fact, I have also pointed this fact on my article on <a href="http://javarevisited.blogspot.com/2011/04/synchronization-in-java-synchronized.html" rel="nofollow">Java synchronization</a>. I completely agree with you that beginners can pick up javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18759983237823334162014-08-16T15:38:53.812-07:002014-08-16T15:38:53.812-07:00Hi Javin,
I would like to point one mistake in th...Hi Javin,<br /><br />I would like to point one mistake in this code, since I used the example above in one of my interviews and the interviewer took my case because he was very unhappy with the performance of the above code, even after reordering the locks.<br /><br />When you sue the synchronized block and obtain the lock on something like Integer.class or String.class, you are blocking the Bhavya Shahnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16389037707693798742014-06-10T13:32:26.733-07:002014-06-10T13:32:26.733-07:00Perhaps synchronizing the method itself is the bes...Perhaps synchronizing the method itself is the best way to avoid deadlock situations rather than block synchronization and ordering...<br /><br />UmaAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36945573555469074262014-06-09T03:58:06.465-07:002014-06-09T03:58:06.465-07:00Beginner in java:
I really can't get the dif...Beginner in java: <br /><br />I really can't get the difference between the deadlock version example program, and fixed version example program. Please clarify.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73822079484592909232014-02-24T10:21:45.470-08:002014-02-24T10:21:45.470-08:00race condition is when two or more thread trying t...race condition is when two or more thread trying to use the same block of code/method... i.e racing each other to use particular resoruce and that is locked by one or other thread. <br /><br />Race condition is sympton of deadlock and go hands in hand. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15344287386565411232013-09-22T20:28:23.654-07:002013-09-22T20:28:23.654-07:00hi all, can you explain what is the diff between ...hi all, can you explain what is the diff between race condition & deadlock? & how can deadlock be preventive?<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10084949537279858002013-05-01T20:33:44.424-07:002013-05-01T20:33:44.424-07:00Just remember, how critical is to write a deadlock...Just remember, how critical is to write a deadlock free concurrent application in Java, because only way to break deadlock is to restart the server. If you could create deadlock detector at runtime, which can also break deadlock as and when they happen, may be by intruppting or killing thread, without losing data invariant than, it would be just fantastic.Steynnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55171839996841166792013-04-30T00:26:27.404-07:002013-04-30T00:26:27.404-07:00If you talk about How to detect deadlock in Java, ...If you talk about How to detect deadlock in Java, then there is a cleaver way by using ThreadMXBean, which provides a method called findDeadLockthreads() which returns id of all those thread which are in deadlock and waiting for each other to release monitor or acquire lock. here is the code :<br /><br />ThreadMXBean threadMBean = ManagementFactory.getThreadMXBean();<br /> <br />Gauravnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37548415249988252012013-04-03T20:07:44.635-07:002013-04-03T20:07:44.635-07:00Imposing ordering is an example of avoiding deadlo...Imposing ordering is an example of avoiding deadlock by braking "Circular Wait" condition. As you know, in order for a deadlock to occur, four condition must met :<br /><br />1) Mutual Exclusion<br />2) Hold and Wait<br />3) No Preemption<br />4) Circular Wait<br /><br />Though you can break any of these conditions to avoid deadlock, it's often easy to break circular wait. But, evenRavinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24204395975457135412013-03-12T22:54:56.097-07:002013-03-12T22:54:56.097-07:00You can also use TIMED and POLLED locks from Reent...You can also use TIMED and POLLED locks from ReentrantLock to have a probabilistic deadlock avoidation. By using timed and polled lock acquision approach, one thread will eventually back-off, giving another thread to either acquire or release the lock. Though this is not guaranteed, it certainly helps to reduce probability of deadlock in Java program.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48879549154386887752013-01-07T04:15:12.504-08:002013-01-07T04:15:12.504-08:00Hi Pallavi,not a problem, threads in Java are alwa...Hi Pallavi,not a problem, threads in Java are always confusing. original order of acquring lock in two methods are opposite e..g in method 1 its lock 1-->lock2 while in method 2 its lock2-->lock1 which can result in deadlock because if two thread calls method 1 and method 2 thread 1 may end of lock 1 and thread 2 end of lock 2 and both will wait for other locks, i.e. deadlock.<br /><br />ByJavin @ CyclicBarrier Example Javahttp://javarevisited.blogspot.sg/2012/07/cyclicbarrier-example-java-5-concurrency-tutorial.htmlnoreply@blogger.com