Java Multithreading Interview Questions with Answers
1. What is the Race Condition in Java? How will you find and solve it?
This is one of the most common questions that mostly appears in senior-level interviews. The interviews may ask about the recent race condition you’ve faced, how to solve it, and even ask you to write a sample code to detect race condition. The race condition is a type of concurrency issue which arises due to the parallel execution of multiple threads at the same time. As Java is a multi-threaded programming language, the risk of Race condition is higher. It’s basically a condition that occurs when two threads operate on the same object without proper synchronization.
2. What are the differences between the wait and sleep methods in Java?
Let’s have a look at another common Java multithreading interview question. If your concept is clear, you won’t face any problem in answering this question. Both are used to pause the execution of a particular thread in a multi-threaded environment. The only difference between wait() and sleep() methods is — whenever a thread calls wait() method, it releases the lock or monitors it holds while when the sleep() method is called, it doesn’t release the lock or monitor it holds. The wait() method is used for inter-thread communication and sleep() is used to pause execution.
3. Suppose, you have thread T1, T2, and T3. How will you ensure that thread T2 will run after T1 and thread T3 after T2?
Most of the time, Interviewer ask this question in the first round of interview to check whether the candidate is familiar with the concept of join() method. You can simply answer this multi-threading question by saying — it can be achieved by using the join() method of the Thread class. Recommended: 5 Important Things Java Programmers Should Learn in 2019
4. Why do we call the start() method first, which in turn calls the run() method, why not directly call the run() method in our programs?
This is another classic question frequently asked by the interviewers. It’s usually asked in junior and medium-level Java interviews. You can answer this question in this way. When you call the start() method, it creates a new thread and executes the code contained by the run() method, But if we directly call the run(), it doesn’t care to create any thread and executes the code on the same calling thread. So, the run() method would behave just like a normal method and we would not be able to take advantage of multithreading
5. How will you awake a blocked thread in Java?
Awaking a blocked thread can mean various things in Java. That’s why experts consider this a tricky question. If the thread is blocked because of calling the wait(), sleep() or join() method, you can interrupt the thread and awake it by throwing InterruptedException. But if the thread is blocked on IO, I don’t think there’s a way to wake it up. I hope you found this article useful. If you know any other common Java multithreading question that should be present in this list, do let us know in the comments below. And lastly, I wish you luck with your future Java interviews.