Top 15 Java Multithreading, Concurrency Interview Questions With Investment Banks
Top 15 Java Multithreading, Concurrency Interview Questions With Investment Banks
Want to learn more about the common Java-related interview questions? Check out this article to learn more about the top interview questions for investment banks.
Join the DZone community and get the full member experience.Join For Free
Multithreading and concurrency questions are an essential part of any Java interview. If you are going for an interview with an investment bank, e.g. Barclays, Citibank, Morgan Stanley for an equities front office Java developer position, you can expect a lot of multithreading interview questions. Multithreading and concurrency are popular topics on investment banking interviews, especially on electronic trading development jobs where they grill candidates on the many tricky Java thread interview questions. They want to ensure that the candidate has a solid knowledge of multithreading and concurrent programming in Java, because most of them are in the business of performance which provides them a competitive advantage.
For example, high volume and low latency electronic trading systems, which are used for Direct to Market (DMA) trading, are usually concurrent in nature. Most of the time, they will focus on microsecond latency, which is why a good knowledge of how to effectively minimize latency and improve throughput is important.
These are some of my favorite thread interview questions about Java. I am not providing an answer to these thread interview questions, but I will give you a hint whenever possible. I will update the post further with detailed answers, like I did for my recent post 10 Singleton interview questions in Java.
After the introduction of the concurrency package in JDK 1.5, there were some questions on concurrent utility and concurrent collections, which were increased, e.g. ThreadLocal, BlockingQueue, Counting Semaphore and ConcurrentHashMap become popular.
The same is true for Java 8 and Java 9. There were questions on lambda expressions, parallel streams, new fork-join pool, and CompletableFuture, which is on the rise in 2018 and will remain in 2019. Hence, you should be prepared for those topics.
15 Java Thread Interview Questions and answers
Anyway, without further ado, here is my list of some of the frequently asked Java multithreading and concurrency questions from Java developer interviews on investment banks, e.g. Barclays, Morgan Stanley, Citibank, etc.
1) You have thread T1, T2, and T3. How will you ensure that thread T2 is run after T1 and thread T3 after T2?
This thread interview question is mostly asked in the first round or phone screening round of an interview and purpose of this multi-threading question is to check whether the candidate is familiar with the concept of "join" method or not. The answer to this multi-threading question is simple — it can be achieved by using the
joinmethod of Thread class.
2) What is the advantage of the new Lock interface over a synchronized block in Java? You need to implement a high-performance cache, which allows multiple readers, but how will you implement the single writer to keep the integrity?
The major advantage of lock interfaces on multithreaded and concurrent programming is that they provide two separate locks for reading and writing, which enables you to write high-performance data structures, like ConcurrentHashMap and conditional blocking.
This Java thread interview question is getting increasingly popular and more and more follow-up questions come based on the answer of the interviewee.
I would strongly suggest reading locks before appearing for any Java multithreading interview, because, nowadays, it's heavily used to build a cache for an electronic trading system on a client and exchange connectivity space.
3) What are differences between wait and sleep method in Java?
Let's take a look at another frequently-asked thread interview question in Java. This question will mostly appear in a phone interview. The only major difference is to wait to release the lock or monitor, while sleep doesn't release any lock or monitor while waiting. The wait is used for inter-thread communication, since sleep is used to introduce pause on execution. See my post wait vs sleep in Java for further information.
4) Write code to implement blocking queue in Java?
This is a relatively tough Java multithreading interview question that serves many purposes. It checks whether a candidate can actually write Java code using thread or not. It sees how good a candidate is on understanding concurrent scenarios, and you can ask a lot of follow-up question based upon his code. If he uses the wait() and notify() method to implement a blocking queue, once the candidate successfully writes it, you can ask him to write it again using new Java 5 concurrent classes, etc.
5) Write code to solve the produce consumer problem in Java? (solution)
Similar to the above questions on the thread, this question is more classic in nature, but sometimes an interviewer will ask follow up questions, like "How do you solve the producer consumer problem in Java?" Well, it can be solved in multiple ways. I have shared one way to solve the producer-consumer problem using BlockingQueue in Java, so be prepared for a few surprises. Sometimes, they even ask you to implement a solution of dining the philosopher problem, as well.
6) Write a program that will result in a deadlock. How will you fix deadlock in Java?
This is my favorite Java thread interview question, because, even though deadlock is quite common while writing a multithreaded concurrent program, many candidates are not able to write deadlock-free code, and they simply struggle.
Just ask them if you have N resources and N threads to complete an operation; then, you require all resources.
Here N can be replaced with two for the simplest case and higher numbers to make the question more intimidating. See How to avoid deadlock in Java for more information on the deadlock.
7) What is an atomic operation? What are atomic operations in Java?
This is a simple Java thread interview question. Another follow-up question would be: do you need to synchronize an atomic operation? You can read more about Java synchronization here.
8) What is a volatile keyword in Java? How do you use it? How is it different from the synchronized method in Java?
Thread questions based on a volatile keyword in Java has become more popular after changes made on it for Java 5 and the Java memory model. It's good to prepare for how volatile variables ensures visibility, ordering, and consistency in a concurrent environment.
9) What is a race condition? How will you find and solve race condition?
Another multithreading question in Java appears mostly on senior-level interviews. Most interviewers ask about a recent race condition that you have faced, how to solve it, and sometimes they will write sample code and ask you to detect the race condition. See my post on the race condition in Java for more information. In my opinion, this is one of the best Java thread interview questions and can really test the candidate's experience on solving race conditions or writing code that is free of data race or any other race condition. The best book about topic is "Concurrency practices in Java.'"
10) How will you take thread dump in Java? How will you analyze Thread dump?
In UNIX, you can use kill -3 and then the thread dump will print the log on windows that you can use "CTRL+Break." While this is a rather simple thread interview question, it can get tricky if they ask you how to analyze it. A thread dump can be useful to analyze deadlock situations, as well.
11) Why do we call start() method which in turns calls run() method, why not we directly call run() method?
This is another classic Java multithreading interview question. Originally, I had some doubt when I started programming in the thread. Nowadays, I am mostly asked in phone interviews or the first round of interview questions at mid and junior-level Java interviews.
Here is the answer to this question. When you call the
start() method, it creates a new thread and executes code declared in the
run() while directly calling the
run() method. This doesn't create any new threads and executes code on the same calling thread. Read my post Difference Between Start and Run Method in Thread for more details.
12) How will you awake a blocked thread in Java?
This is a tricky question on threading. Blocking can result in many ways — if the thread is blocked on IO, then, I don't think there is a way to interrupt the thread. Let me know if there is any. On the other hand, if a thread is blocked due to the result of calling the
sleep() , or
join() method, you can interrupt the thread, and it will awake by throwing
InterruptedException. See my post called How to Deal With Blocking Methods in Java for more information on handling blocked thread.
13) What is the difference between CyclicBarriar and CountdownLatch in Java? ( answer)
New Java thread interview questions mostly check your familiarity with JDK 5 concurrent packages. One difference is that you can reuse the
CyclicBarrier once the barrier is broken, but you can not reuse
CountDownLatch. If you want to learn more, check out the Multithreading and Parallel Computing in Java course on Udemy.
14) What is an immutable object? How does it help in writing a concurrent application?
While this interview question does not directly relate to the thread, it indirectly helps a lot. This interview question can become more tricky if they ask you to write an immutable class or ask you Why String is Immutable in Java as a follow-up.
15) What are some common problems you have faced in multi-threading environment? How did you resolve it?
Memory-interference, race conditions, deadlock, livelock, and starvation are an example of some problems that come with multithreading and concurrent programming. There is no end of a problem; if you get it wrong, they will be hard to detect and debug.
This is mostly an experience-based interview question about Java. You can see Java Concurrency in Practice Course by Heinz Kabutz for some real-world problems faced in actual high-performance multithreaded applications.
These were some of my favorite Java thread interview questions and commonly-asked questions by investment banks. This list is by no means complete, so please comment below some of the interesting Java thread questions that you have faced during an interview. This article collects and shares great interview questions on the multithreading concept, which not only helps in the interview but opens the door for learning a new threading concept.
One of the Java-revisited readers, Hemant, has contributed some more thread interview questions in Java. Here are those additional questions:
1) Difference between green thread and native thread in Java?
2) Difference between thread and process? (answer)
3) What is context switching in multi-threading?
4) Difference between deadlock and livelock, deadlock and starvation?
5) What thread-scheduling algorithm is used in Java?
6) What is thread-scheduler in Java?
7) How do you handle an unhandled exception in the thread?
8) What is thread-group, why its advised not to use thread-group in Java?
9) Why is the Executor framework better than creating and managing threads via the application?
10) Difference between Executor and Executors in Java? (answer)
11) How to find which thread is taking maximum CPU in windows and Linux server?
Published at DZone with permission of Javin Paul , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.