f you are going for java interview on any Investment bank expect lots of muti-threading interview questions on your way. Multi-threading is a favorite topics on Investment banking specially on electronic trading development and they grill candidate on many confusing java thread interview questions. They just want to ensure that the guy has solid knowledge of multi-threading and concurrent programming in java because most of them are in business of performance. High volume low latency Electronic trading System which is used for Direct to Market (DMA) trading is usually concurrent in nature. These are my favorite thread interview questions on java asked on different on different time. I am not providing answer of these thread interview questions but I will give you hint whenever possible. Answer of these java thread interview questions can be found by doing Google.
Java Multi-threading Interview questions answers:
More important is to understand the concept behind these multi-threading questions simply mugging the answers of thread interview questions is not going to help because there would be a lot of follow-up questions based upon your answer and if you haven't master the particular thread topic it would be difficult. Threading in Java is very interesting topic even more after Java 5 which has added lot of concurrency classes and now days interview on java thread are mostly focus around this new concurrent utilities like Executor framework, Countdown Latch, Atomic classes, Re-entrant and ReadWriteLock and Concurrent Collection classes.
15 Java Thread Interview Questions and answers
1) You have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2?
java thread interview questionsThis thread interview questions is mostly asked in first round or phone screening round of interview and purpose of this multi-threading question is to check whether candidate is familiar with concept of "join" method or not. Answer of this multi-threading questions is simple it can be achieved by using join method of Thread class.
2) What is the advantage of new Lock interface over synchronized block in Java? You need to implement a high performance cache which allows multiple reader but single writer to keep the integrity how will you implement it?
The major advantage of lock interfaces on multi-threaded and concurrent programming is they provide two separate lock for reading and writing which enables you to write high performance data structure like concurrenthashmp and conditional blocking. This java threads interview question is getting increasingly popular and more and more follow-up questions come based upon answer of interviewee. I would strongly suggest reading Locks before appearing for any java multi-threading interview because now days Its heavily used to build cache for electronic trading system on client and exchange connectivity space.
3) What are differences between wait and sleep method in java?
Another classic interview question on java thread mostly asked in phone interview. Only major difference is wait release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution.
4) Write code to implement blocking queue in Java?
This java multi-threading interview question servers many purpose , it checks whether candidate can actually write java code using thread or not, it sees how good interviewee is on understanding scenarios of multi-threading and you can ask lot of follow-up question based upon his code. If he uses wait() and notify() method to implement blocking queue, Once interviewee successfully writes it you can ask him to write it again using new java 5 concurrent classes.
5) Write code to solve the Produce consumer problem in Java?
Similar to above questions on thread but more classic in nature, some time interviewer ask follow up questions as what happen if you have multiple Producer and single consumer or vice-versa , so be prepare for surprises. Some time they even ask to implement solution of dining philosopher problem as well.
6) Write a program which will result in 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 multi-threaded concurrent program many candidates not able to write deadlock free code and they simply struggle. Just ask them you have n resources and n thread and to complete an operation you require all resources. Here n can be replace with 2 for simplest case and higher number to make question more intimidating. To read more about deadlock in java see the link.
7) What is atomic operation? What are atomic operations in Java?
Simple java thread interview questions, another follow-up is do you need to synchronized an atomic operation? :) You can read more about java synchronization here.
8) What is volatile keyword in Java? How to use it? How is it different from synchronized method in Java?
Thread questions based on volatile keyword in Java has become more popular after changes made on it on Java 5 and Java memory model. It’s good to prepare well about how volatility ensures visibility, ordering and consistency.
9) What is race condition? How will you find and solve race condition?
Another classic java threading interview questions and mostly interviewer grill on recent race condition you have faced and how did you solve it and some time they will write sample code and ask you detect race condition. In my opinion this is one of the best java thread interview question and can really test the candidate's experience on solving race condition or writing code which is free of data race or any other race condition. Best book to get mastery of this 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 thread dump will print on log on windows you can use "CTRL+Break". Rather simple and focus thread interview question but can get tricky if he ask how you analyze it.
11) Why we call start() method which in turns calls run method, why not we directly call run method ?
Another classic java multi-threading interview question This was my original doubt when I started programming in thread. Now days mostly asked in phone interview or first round of interview at mid and junior level java interviews.
12) How will you awake a blocked thread in java?
This is tricky question blocking can result on many ways, if 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 thread is blocked due to result of calling wait(), sleep() or join() method you can interrupt the thread and it will awake by throwing Interrupted Exception.
13) What is difference between CyclicBarriar and Countdown Latch in Java ?
New java thread interview questions mostly to check familiarity with JDK 5 concurrent packages.
14) What is immutable object? How does it help on writing concurrent application?
Another classic interview questions on multi-threading, not directly related to thread but indirectly helps a lot. This java interview question can become more tricky if ask you to write an immutable class or ask you Why String is immutable in Java as 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, live lock and starvation are example of some problems comes in multi-threading and concurrent programming. There is no end of problem if you get it wrong and they will be hard to detect and debug. This is mostly experienced based interview question on java thread instead of fact based.
Java Multi-threading Interview questions answers:
More important is to understand the concept behind these multi-threading questions simply mugging the answers of thread interview questions is not going to help because there would be a lot of follow-up questions based upon your answer and if you haven't master the particular thread topic it would be difficult. Threading in Java is very interesting topic even more after Java 5 which has added lot of concurrency classes and now days interview on java thread are mostly focus around this new concurrent utilities like Executor framework, Countdown Latch, Atomic classes, Re-entrant and ReadWriteLock and Concurrent Collection classes.
15 Java Thread Interview Questions and answers
1) You have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2?
java thread interview questionsThis thread interview questions is mostly asked in first round or phone screening round of interview and purpose of this multi-threading question is to check whether candidate is familiar with concept of "join" method or not. Answer of this multi-threading questions is simple it can be achieved by using join method of Thread class.
2) What is the advantage of new Lock interface over synchronized block in Java? You need to implement a high performance cache which allows multiple reader but single writer to keep the integrity how will you implement it?
The major advantage of lock interfaces on multi-threaded and concurrent programming is they provide two separate lock for reading and writing which enables you to write high performance data structure like concurrenthashmp and conditional blocking. This java threads interview question is getting increasingly popular and more and more follow-up questions come based upon answer of interviewee. I would strongly suggest reading Locks before appearing for any java multi-threading interview because now days Its heavily used to build cache for electronic trading system on client and exchange connectivity space.
3) What are differences between wait and sleep method in java?
Another classic interview question on java thread mostly asked in phone interview. Only major difference is wait release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution.
4) Write code to implement blocking queue in Java?
This java multi-threading interview question servers many purpose , it checks whether candidate can actually write java code using thread or not, it sees how good interviewee is on understanding scenarios of multi-threading and you can ask lot of follow-up question based upon his code. If he uses wait() and notify() method to implement blocking queue, Once interviewee successfully writes it you can ask him to write it again using new java 5 concurrent classes.
5) Write code to solve the Produce consumer problem in Java?
Similar to above questions on thread but more classic in nature, some time interviewer ask follow up questions as what happen if you have multiple Producer and single consumer or vice-versa , so be prepare for surprises. Some time they even ask to implement solution of dining philosopher problem as well.
6) Write a program which will result in 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 multi-threaded concurrent program many candidates not able to write deadlock free code and they simply struggle. Just ask them you have n resources and n thread and to complete an operation you require all resources. Here n can be replace with 2 for simplest case and higher number to make question more intimidating. To read more about deadlock in java see the link.
7) What is atomic operation? What are atomic operations in Java?
Simple java thread interview questions, another follow-up is do you need to synchronized an atomic operation? :) You can read more about java synchronization here.
8) What is volatile keyword in Java? How to use it? How is it different from synchronized method in Java?
Thread questions based on volatile keyword in Java has become more popular after changes made on it on Java 5 and Java memory model. It’s good to prepare well about how volatility ensures visibility, ordering and consistency.
9) What is race condition? How will you find and solve race condition?
Another classic java threading interview questions and mostly interviewer grill on recent race condition you have faced and how did you solve it and some time they will write sample code and ask you detect race condition. In my opinion this is one of the best java thread interview question and can really test the candidate's experience on solving race condition or writing code which is free of data race or any other race condition. Best book to get mastery of this 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 thread dump will print on log on windows you can use "CTRL+Break". Rather simple and focus thread interview question but can get tricky if he ask how you analyze it.
11) Why we call start() method which in turns calls run method, why not we directly call run method ?
Another classic java multi-threading interview question This was my original doubt when I started programming in thread. Now days mostly asked in phone interview or first round of interview at mid and junior level java interviews.
12) How will you awake a blocked thread in java?
This is tricky question blocking can result on many ways, if 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 thread is blocked due to result of calling wait(), sleep() or join() method you can interrupt the thread and it will awake by throwing Interrupted Exception.
13) What is difference between CyclicBarriar and Countdown Latch in Java ?
New java thread interview questions mostly to check familiarity with JDK 5 concurrent packages.
14) What is immutable object? How does it help on writing concurrent application?
Another classic interview questions on multi-threading, not directly related to thread but indirectly helps a lot. This java interview question can become more tricky if ask you to write an immutable class or ask you Why String is immutable in Java as 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, live lock and starvation are example of some problems comes in multi-threading and concurrent programming. There is no end of problem if you get it wrong and they will be hard to detect and debug. This is mostly experienced based interview question on java thread instead of fact based.