Interview Questions

What is race condition in multithreading and how can we solve it?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

13. What is race condition in multithreading and how can we solve it?

This is very important question, this forms the core of multi threading, you should be able to explain about race condition in detail. When more than one thread try to access same resource without synchronization causes race condition.
So we can solve race condition by using either synchronized block or synchronized method. When no two threads can access same resource at a time phenomenon is also called as mutual exclusion.

Few sub questions
What if two threads try to read same resource without synchronization?
When two threads try to read on same resource without synchronization, it’s never going to create any problem.

What if two threads try to write to same resource without synchronization?
When two threads try to write to same resource without synchronization, it’s going to create synchronization problems.

(Continued on next question...)

Other Interview Questions