How to avoid a Deadlock while writing Concurrent Programs — Java Example

How to avoid a Deadlock while writing Concurrent Programs — Java Example

hackernoon.com hackernoon.com4 years ago in #Dev Love53

Originally published by Javin Paul on December 4th 2018 How to avoid a Deadlock while writing Concurrent Programs — Java Example Multithreading and Parallel Computing in Java Hello guys, do you want to learn how to avoid deadlock while writing concurrent programs, especially in Java? Writing correct concurrent application is never easy as you have to deal with several concurrency issues like race conditions, livelock, deadlock, memory interference etc. In this article, I’ll tell you how to avoid deadlock while writing a concurrent program as an interview question discussion. Since I am Java developer, I’ll explain the concept and implementation in Java but the technique I’ll share with you is valid for all kind of programming languages like C or C# etc. The question of avoiding deadlock is one of the popular Java interview questions, with multi-threading being asked mostly at a senior level interview with lots of follow up questions. Even though the question looks very basic, most of the Java developers get stuck once you start going deeper. So, what is a deadlock? The answer is simple — when two or more threads are waiting for each other to release the resource they need (lock) and get stuck for infinite time, the situation is called deadlock. It will only happen in the case of multitasking or multi-threading, because that’s where multiple threads come into the picture. 1. How Do you detect Deadlock in Java? Though this could have many answers, first, I would look at the code to see if a nested synchronized block is calling a synchronized method from another or if it is trying to get a lock on a different object. If that is the case, there is a good chance of deadlock, if the developer is not careful. Another way to determine deadlock risks is when you actually get dead-locked while running the application. If this happens, try to take a thread dump, in Linux you can do this by the command “kill -3.” This will print the status of all threads in an application log file, and you can see which thread is locked on which object. You can analyze that thread dump with using tools like fastthread.io, which allows you to upload your thread dump and analyze it. Another way is to use the jConsole/VisualVM. It will show you exactly which threads are getting locked and on which object. If you are interested in learning about troubleshooting tools and the process to analyzing your thread dump, I suggest you take a look at this Analyzing Java Thread Dumps course on Pluralsight by Uriah Levy. This is an advanced practice course to learn more about Java thread dump and familiarize you with other popular advanced troubleshooting tools. Analyzing Java Thread Dumps Btw, you would need a Pluralsight membership to get access this course, which cost around $29 per month or $299 annually (14% discount). If you don’t have Pluralsight membership, I encourage you to get one because it allows you to access their 5000 online courses on all latest topics like front-end and back-end development, machine learning etc. It also include interactive quizzes, exercises, and latest certification material .  » Read More

Like to keep reading?

This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply