How to Avoid Deadlock in Java?

Swatee Chand
Edureka
Published in
4 min readAug 30, 2019

--

Java programming language supports multithreading. It involves multiple threads running simultaneously for multitasking. But in certain cases or due to certain shortcomings, the threads find themselves in the waiting state forever. In this article, We will understand the deadlock condition in Java and different ways to avoid it. The following are the topics discussed in this article:

  • What is Deadlock in Java?
  • Deadlock Example
  • How To Avoid Deadlock in Java?

What is Deadlock in Java?

Deadlock in Java is a condition where two or more threads are blocked forever, waiting for each other.

This usually happens when multiple threads need the same locks but obtain them in different orders. Multithreaded Programming in Java suffers from the deadlock situation because of the synchronized keyword.

It causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object.

Deadlock Example

public class Example
{
public static void main(String[] args)
{
final String r1 = "edureka";
final String r2 = "java";
Thread t1 = new Thread()
{
public void run()
{
synchronized(r1)
{
System.out.println("Thread 1: Locked r1");
try
{ Thread.sleep(100);} catch(exception e) {}
synchronized(r2)
{
System.out.println("Thread 1: Locked r2");
}
}
}
};
Thread t2 = new Thread()
{
public void run()
{
synchronized(r1)
{
System.out.println("Thread 2: Locked r1");
try{ Thread.sleep(100);} catch(exception e) {}
synchronized(r2)
{
System.out.println("Thread 2: Locked r2");
}
}
}
};
t1.start();
t2.start();
}
}

Output:
Thread 1: Locked r1
Thread 2: Locked r2

How To Avoid Deadlock in Java?

Although it is not completely possible to avoid deadlock condition, but we can follow certain measures or pointers to avoid them:

  • Avoid Nested Locks — You must avoid giving locks to multiple threads, this is the main reason for a deadlock condition. It normally happens when you give locks to multiple threads.
  • Avoid Unnecessary Locks — The locks should be given to the important threads. Giving locks to the unnecessary threads that cause the deadlock condition.
  • Using Thread Join — A deadlock usually happens when one thread is waiting for the other to finish. In this case, we can use Thread.join with a maximum time that a thread will take.

This brings us to the end of this article where we have learned about the deadlock in Java and how to avoid it. I hope you are clear with all that has been shared with you in this tutorial.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Java Tutorial

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. What is the concept of String Pool in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. Java Tutorial

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on August 30, 2019.

--

--