Java Multithreading Interview Questions and Answers

61. What are the conditions to occur deadlock situation in a program?

Ans: A deadlock situation in a program may occur due to the following conditions. They are as follows:

  • Mutual Exclusion
  • Hold and Wait
  • No Preemption
  • Circular Wait

62. How to avoid deadlock in Java program?

Ans: To prevent (or avoid) a deadlock from occurring in a program at least one condition for a deadlock should be removed:

  • Mutual exclusion
  • Resource holding
  • No preemption
  • Circular wait

For more detail, go to this tutorial: Deadlock in Java | Realtime Example

63. How can we detect a deadlock situation in Java program?

Ans: Use ThreadMXBean.findDeadlockedThreads() method to detect deadlock situation in Java program.

64. Which is better to use: synchronized method or synchronized block, if we need to synchronize a certain block of code?

Ans: Synchronized block is better to use for synchronization on a certain block of code or statements inside the method.

65. Can we declare a constructor as synchronized in Java?

Ans: No. We cannot declare a constructor as synchronized. This will lead to a compiler time error.

Hope that this tutorial has covered all types of multithreading interview questions in java with the best possible answers. Interview questions based on Java multithreading will be regularly updated further.
All the best!!!