Top 12 Java Thread, Concurrency and Multithreading Interview Questions For experienced

Java concurrency and thread interview questions answers
Multithreading is an important feature of Java programming language, which means threads are also an important part of any Java interview. It's true and in fact at beginners and freshers level Thread interview questions in Java are one of most difficult to answer. One reason for interview question related to multithreading and concurrency being difficult is confusion around how multiple threads works together and second is threads are genuinely a complicated topic to understand and use correctly. Mostly thread interview questions checks Java programmers knowledge on Java Thread API, Java concurrency API, issues related to multi-threading like a race condition, thread-safety, and deadlock. Some time multithreading and concurrency interview question also focus on parallel design patterns like solving the producer-consumer problem, implementing work steal pattern or solving dining philosopher problem in Java. This is especially true while interviewing experienced Java developers with 4 to 6 years of experience.

In this article, we will take a look at different kinds of multithreading and concurrency questions asked in various interviews e.g. on telephonic or face to face interview, on written test, to both experienced and senior Java developers, and some tips to answer them correctly.

Questions asked on telephonic or the first round of interviews are tend to be easier and you must answer them to the point with a keyword, which interviewer is expecting. On the face to face interview, be prepare for different kinds of follow-up questions. Books like Java Programming Interview exposed also helps you to prepare better and answer to the point.

thread, concurrency and multi-threading questions for experienced Java programmers

Java Interview questions on Concurrency and multithreading

As I said, in this Java article, not only,  I will share some of the most commonly asked thread interview questions at freshers and beginners level, like up to 2 to 4 years of experience, and some tips and tricks to answer them correctly.

By the way, these thread interview questions are equally useful for senior Java developers or guys with some Java experience in hand. I have tried to share answers to these interview questions on the thread as well but I suggest you do some research and learn the topic well to answer any follow-up questions, which comes due to your response to these thread questions in Java.

Anyway here is my collection of Java thread interview questions and how to answers them in Java :

1) What is the difference between start and run method in Java Thread? (answer)
This thread interview question is also ask as if start() method eventually call run() method then why do you need to call start() method, why not call run() method directly. well, reason is that because start method creates a new thread and call the code written inside the run method on a new thread while calling run method executes that code on the same thread. You can also see the article start vs run method in Java for more details.

2) Write code to avoid deadlock in Java where N threads are accessing N shared resources? (answer)
This is a classic Java multithreading interview questions, which appears on almost every list of Java thread questions. This question is based on risk and issues faced by parallel programs without proper synchronization or incorrect synchronization. This question explores the concept of looking and best practices on acquiring and releasing the lock on shared resource. By the way, it's been covered in many places as well and I suggest reading  How to prevent deadlock in Java, not only for detail answer of this Java multithreading question but also to learn how to prevent deadlock in Java.

3) Which one is better to implement thread in Java ? extending Thread class or implementing Runnable? (answer)
Well, this is another frequently asked questions on any Java thread interview. Essentially these are two way to implement Thread in Java, by extending java.lang.Thread class or by implementing java.lang.Runnable interface.

By extending the class you are using your chance to extend one any only one class as Java does not support multiple inheritances, by implementing a Runnable interface you can still extend another class. So extending Runnable or even Callable is a better choice. You can also see Runnable vs Thread class in Java for more answers on this questions.

Given its simplicity and fact-based nature, this question mostly appears on either telephonic round or initial screening rounds. Key points to mention, while answering this question includes multiple inheritance at the class level and separation of defining a task and execution of a task. Runnable only represent a task, while Thread represent both tasks and it's execution.

4) What is Busy Spinning? Why will you use Busy Spinning as wait strategy? (answer)
This is one of the advanced concurrency interview questions in Java and only asked to experienced and senior Java developers, with lots of concurrent coding experience under the belt. By the way, the concept of busy spinning is not new, but its usage with multi-core processor has risen recently (see The Art of Multiprocessor Programming).

Java Thread interview question and answers for beginners freshers and experienced programmer

The busy waiting is a wait strategy, where one thread wait for a condition to become true, but instead of calling wait or sleep method and releasing CPU, it just spins. This is particularly useful if the condition is going to be true quite quickly i.e. in a millisecond or microsecond.

The advantage of not releasing CPU is that all cached data and instruction remain unaffected, which may be lost, had this thread is suspended on one core and brought back to another thread. If you can answer this question, that rest assure of a good impression.

5) What is the difference between CountDownLatch and CyclicBarrier in Java? (answer)
The CountDownLatch and CyclicBarrier in Java are two important concurrency utility which is added on Java 5 Concurrency API. Both are used to implement scenario, where one thread has to wait for other thread before starting processing but there is a difference between them.

The key point to mention, while answering this question is that CountDownLatch is not reusable once the count reaches to zero, while CyclicBarrier can be reused even after the barrier is broken.

You can also see my previous article difference between CyclicBarrier and CountDownLatch in Java for a more detailed answer of this concurrency interview question and a real life example of where to use these concurrency utilities.

6) What is the difference between wait and sleep in Java? (method)
One more classic Java multithreading question from the telephonic round of interviews. The key point to mention while answering this question is to mention that wait will release the lock and must be called from the synchronized context, while sleep will only pause the thread for some time and keep the lock.

By the way, both methods throw IntrupptedException and can be interrupted, which can lead to some follow-up questions like, can we awake a sleeping or waiting for a thread in Java? You can also read a detailed answer on my post of same title here.

thread interview questions with answers

7) How do you solve producer consumer problem in Java? (solution)
One of my favorite questions during any Java multithreading interview, Almost half of the concurrency problems can be categorized in the producer-consumer pattern. There are basically two ways to solve this problem in Java, One by using wait and notify method and other by using BlockingQueue in Java.  later is easy to implement and a good choice if you are coding in Java 5.

The key points to mention, while answering this question is thread-safety and blocking nature of BlockingQueue and how that helps, while writing concurrent code. You can also expect lots of follow-up questions including, what happen if you have multiple producer or multiple consumers, what will happen if a producer is faster than consumer thread or vice-versa. You can also see this link for example of how to code producer-consumer design in Java using blocking queue

8) Why is ConcurrentHashMap faster than Hashtable in Java? (answer)
ConcurrentHashMap is introduced as an alternative of Hashtable in Java 5, it is faster because of its design. ConcurrentHashMap divides the whole map into different segments and only lock a particular segment during the update operation, instead of Hashtable, which locks whole Map.

The ConcurrentHashMap also provides lock-free read, which is not possible in Hashtable, because of this and lock striping, ConcurrentHashMap is faster than Hashtable, especially when a number of the reader is more than the number of writers.

In order to better answer this popular Java concurrency interview questions, I suggest reading my post about the internal working of ConcurrentHashMap in Java.

9) What is the difference between submit() and execute() method of Executor and ExecutorService in Java? (answer)
The main difference between submit and execute method from ExecutorService interface is that former return a result in the form of a Future object, while later doesn't return a result. By the way, both are used to submit a task to thread pool in Java but one is defined in Executor interface,while other is added into ExecutorService interface. This multithreading interview question is also asked in the first round of Java interviews.

10) How do you share data between two threads in Java? (answer)
One more Java multithreading question from the telephonic round of interview. You can share data between thread by using shared object or shared data structures like Queue. Depending upon, what you are using, you need to provide the thread-safety guarantee, and one way of providing thread-safety is using synchronized keyword.

If you use concurrent collection classes from Java 5 e.g. BlockingQueue, you can easily share data without being bothered about thread safety and inter-thread communication. I like this thread question, because of it's simplicity and effectiveness. This also leads further follow-up questions on issues which arise due to sharing data between threads e.g. race conditions.

11) What is ReentrantLock in Java? Have you used it before? (answer)
ReentrantLock is an alternative of synchronized keyword in Java, it is introduced to handle some of the limitations of synchronized keywords. Many concurrency utility classes and concurrent collection classes from Java 5, including ConcurrentHashMap uses ReentrantLock, to leverage optimization.

The ReentrantLock mostly uses an atomic variable and faster CAS operation to provides better performance. Key points to mention are the difference between ReentrantLock and synchronized keyword in Java, which includes the ability to acquire lock interruptibly, timeout feature while waiting for lock etc. ReentrantLock also gives the option to create fair lock in Java.Once again a very good Java concurrency interview question for experienced Java programmers.

12) What is ReadWriteLock in Java? What is the benefit of using ReadWriteLock in Java?(answer)
This is usually a follow-up question of previous Java concurrency questions. The  ReadWriteLock is again based upon the concept of lock striping, one of the advance thread-safety mechanism which advocates separating locks for reading and writing operations (see Concurrent Programming in Java: Patterns and principles by Doug Lea for more details).

If you have noticed before, reading operation can be done without locking if there is no writer and that can hugely improve the performance of any application. The ReadWriteLock leverage this idea and provide policies to allow maximum concurrency level. Java Concurrency API also provides an implementation of this concept as ReentrantReadWriteLock.

Depending upon Interviewer and experience of the candidate, you can even expect to provide your own implementation of ReadWriteLock, so be prepare for that as well.

Here is a nice diagram which clearly explains working of read-write lock in Java:

Java Multithreading and Concurrency Interview Questions with Answers

These were some of my favorite interview questions based on multithreading and concurrent in Java. Threading and Concurrency is a big topic in Java and has lots of interesting, tricky and tough question but for starters and freshers, these questions certainly help to clear any thread interview in Java.

As I said, mentioning the key points are very important while answering questions on multithreading and concurrency.  I also suggest further reading Java Concurrency in Practice to learn more about locking, synchronization, concurrent collections and concurrency utility classes to do well in core Java and multithreading interviews.

Related Java Interview Questions for Experienced Programmers:


  1. One of the good question on multithreading, I have recently faced in a Java interview. After asking some basic question on thread and synchronization e.g. wait, notify, creating thread, stopping thread, thread pool etc. He asked, How do you reduce contention between threads? That's a really good question. As I was familiar with some techniques of reducing contention among multiple thread, I was quick to bring lock stripping concept, which splits a lock into multiple e.g. ReadWrite lock has separate lock for reading and writing. This can significantly reduce contention. Another way to reduce thread contention is minimizing use of synchronized keyword, instead using atomic and volatile variables.

  2. Good question. Some answers are really good, especially with the busy spinning. Keep it up.

  3. your answer to spining is not completely true. you completly ignore the java memory model which seems like an importand part here. it only gurantees you to see the correct value if accessed in sync block, or since 1.5 with some types using volatile. but second problem with not synchronizing is the as if serial semantic of jvm executions. this might lead you to situations where the reference to a object has already been set but the constructor has not been run. so even when your check for ref != null will be true you could get error.
    cheers, peter

  4. Threads can be implemented by extending Thread class, implementing Runnable interface and Callable interface.

    If you want to return an value or throw an exception then use Callable otherwise use Runnable as extending Thread class limits the Class inheritance and also makes the process heavy.

    Below link can be useful if you want to find more:
    Different ways to implement Threads in Java

  5. Few more questions
    - If you are asked to write code for synchronizing between two threads or 100 threads? which code will be harder to write and why?

    - Tell me three problems you usually face on concurrent environment.

    - What happend if you add task into Fixed thread pool and worker queue is full?

    - What happend if an exception is throw into a Thread?

  6. tool is a self explanatory tool for analyzing thread dumps