Image Image Image Image Image Image Image Image Image Image

Mudassir Shahzad | April 26, 2019

Scroll to top



Deadlocks in Java – How to avoid

Deadlocks in Java – How to avoid
Mudassir Shahzad

Deadlocks in Java is one of the tough categories in which interview questions are asked about multithreading, asked more at a senior level and with lots of follow up questions, though question looks very basic but most of developer get stuck once you start going deep.

The question might start with “What is deadlock ?”

The answer is simple, when two or more threads are waiting for each other to release a certain resouce (memory space, i/o resource or a class etc) and get stuck for infinite time , this situation is called deadlock .

Remember, This will only happen in case of multitasking.

How do you detect deadlock in Java ?

Though this could have many answers , my version is first I would look at the code. If I see

  • Nested synchronized blocks or
  • Calling one synchronized method from other or
  • Trying to get lock on different objects

then there is good chance of deadlock if the developer is not very careful.

Another way to find a deadlock in java is when you actually get locked while running the application, try to take thread dump.

In Linux, you can do this by command “kill -3″ , this will print status of all the thread in application log file and you can see which thread is locked on which object.

For windows, you can use jconsole which will show you exactly which threads are locked and on which objects.

Once you answer this, the interviewer might ask you to write code which will result in deadlock ?

Here is one of my version

If method1() and method2() both are called by two or many threads at the same time, then there is a good chance of deadlock because let’s say

thead 1 aquires lock on Sting object while executing method1() and

thread 2 acquires lock on Integer object while executing method2()

Now, both will be waiting for each other to release lock on Integer and String to proceed further which will never happen.

Now the interviewer comes to final and one of the most important part,

How to fix deadlock in java ? or How to avoid deadlock in Java ?

If you have gone through the above code carefully you may have figured out that the real reason for the deadlock is not multiple threads but the way the threads gain lock on objects.

So, if you use an ordered access then the problem will be resolved, here is
the fixed version.

Now there would not be any deadlock because both methods are accessing lock on Integer and String object in the same order. So, if thread A acquires lock on Integer object , thread B will not proceed until thread A releases Integer lock. Similarly, thread A will not be blocked even if thread B holds String lock because now thread B will not expect thread A to release Integer lock to proceed further.

Hope this helps.




If this article is helpful, do share it using the buttons below.


  1. Mansoor Shah

    Nice and very helpful article!

  2. Informative write-up, totally what I needed.

  3. Appreciate this post. Will try it out.

  4. jjj

    Pretty section of content. I just stumbled upon
    your web site and in accession capital to assert that I acquire in fact
    enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently fast.

  5. graco magnum prox9

    A motivating discussion is definitely worth comment.

    I do believe that you should publish more on this topic, it may not be a taboo matter but
    usually people do not talk about such topics. To the next!
    All the best!!

  6. master cleanse

    It’s a pity you don’t have a donate button! I’d most certainly donate to this brilliant blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to brand new updates and will share this site with my Facebook group.
    Chat soon!

  7. master cleanse

    Spot on with this write-up, I really believe that
    this website needs a lot more attention. I’ll probably be back again to see more, thanks for the info!

  8. Clementina Spanswick

    I simply want to mention I am new to weblog and honestly savored you’re web-site. Most likely I’m want to bookmark your blog . You really have exceptional writings. Thank you for sharing with us your website page.

  9. Rupert Solle

    I simply want to say I am just newbie to blogging and site-building and really liked this blog site. Likely I’m want to bookmark your blog post . You actually come with very good posts. Thanks for sharing your web-site.

  10. wagner sprayers

    Quality content is the important to be a focus for the users to visit
    the site, that’s what this website is providing.

  11. Google

    The information and facts talked about in the article are several of the ideal out there

  12. Google

    The facts mentioned within the write-up are some of the top available

  13. hack gmail account

    nice article i loved it..

  14. Epilator reviews 2013

    bookmarked!!, I love your site!

  15. click here

    Loved your take on Deadlocks in Java &! I’m just now getting into it myself.

  16. Stefani Delanoche

    Creating cool blog is not harder than creating great information. But you know that you done both really well. Thank man.

Submit a Comment

Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.