15 top Java multithreaded interview questions (with answers)

  • 2020-05-27 05:37:09
  • OfStack

Questions on multithreading and concurrency are an essential part of any Java interview. If you want to get a front desk position in any stock investment bank, then you should prepare a lot of questions about multithreading. Multithreading and concurrency is a very popular topic in investment banking, especially related to the development of electronic transactions. They ask the interviewer a lot of confusing Java thread questions. The interviewer just wants to make sure that the interviewer has enough knowledge of Java threads and concurrency, because many of the candidates are superficial. High-volume and low-latency electronic trading systems for direct-to-market transactions are concurrent in nature. Here are some of the Java thread questions I like to ask at different times and places. I didn't give you the answers, but I'll give you clues whenever possible, and sometimes those clues are enough to answer the questions. Problems with concurrent tools and concurrent collections are increasing now that Java5 is being referenced and packaged. Among those questions, ThreadLocal, Blocking Queue, Counting Semaphore and ConcurrentHashMap are popular.

15 Java multi-threaded interview questions and answers

1) now there are 33 threads of T1, T2 and T2. How do you ensure that T2 will execute after T1 and T3 will execute after T2?

This threading question is usually asked in the first round or during a phone interview to see if you are familiar with the "join" method. This multithreading problem is relatively simple and can be implemented using the join method.

2) what is the advantage of Lock interface over synchronized block in Java? You need to implement an efficient cache that allows multiple users to read, but only one user to write, in order to maintain its integrity. How do you do that?

The biggest advantage of the lock interfaces in multithreaded and concurrent programming is that they provide locks for both reads and writes, allowing you to write high-performance data structures like ConcurrentHashMap and conditional blocking. The Java thread interview questions are increasingly being asked based on the interviewer's answers. I strongly recommend that you read Locks 1 carefully before you go to a multi-threaded interview, as it is currently heavily used to build the client cache and transaction connection space for the electronic trading end.

3) what are the differences between wait and sleep methods in java?

Java thread interview questions that are often asked during phone interviews. The biggest difference is that wait releases the lock while waiting, while sleep1 holds the lock directly. Wait is usually used for thread interaction, and sleep is usually used for pausing execution.

4) block the queue with Java.

This is a relatively difficult multi-threaded interview question that can accomplish a lot of things. First, it can detect whether candidates can actually write programs using Java threads. Second, you can test the candidate's understanding of the concurrency scenario, and you can ask a lot of questions based on that. If he USES the wait() and notify() methods to block queues, you can ask him to write it again using the latest Java 5 concurrency class.

5) write code with Java to solve producer-consumer problems.

Similar to the above question, but this one is more classic. Sometimes the interview will ask the following questions. How to solve the producer-consumer problem in Java, of course, there are many solutions, and I've already Shared one with blocking queues. Sometimes they even ask how to make it possible for philosophers to eat.

6) use Java to program a program that will cause deadlock, how would you solve it?

This is my favorite Java thread interview question, because even though deadlocks are common when writing concurrent multithreaded programs, many candidates can't write deadlock free code. They are struggling. Just tell them that you have N resources and N threads, and you need all the resources to complete an operation. For the sake of simplicity, n here can be replaced with 2; larger Numbers make the problem look more complicated. Get more information about deadlocks by avoiding them in Java.

7) what is the atomic operation? What is the atomic operation in Java?

Very simple java thread interview question, the next question is that you need to synchronize 1 atomic operation.

8) what is the key role of volatile in Java? How to use it? How does it differ from the synchronized method in Java?

Threading issues based on the volatile keyword have become increasingly popular since the Java 5 and Java memory models were changed. You should be ready to answer questions about how the volatile variable ensures visibility in a concurrent environment.

9) what are the conditions of competition? How do you find and solve competition?

This is a question from the advanced stage of the multithreaded interview. Most interviewers will ask you about your recent competitive situation and how you solved it. Some of the time they'll write simple code and let you detect the race conditions of the code. Refer to my previous post on Java competitive conditions. In my opinion, this is the best java thread interview question 1, which can definitely test the candidate's experience in solving competitive conditions, or writing which is of data race or other race condition. The best book on this subject is Concurrency practices in Java.

10) how will you use thread dump? How would you analyze Thread dump?

In UNIX you can use kill-3, then thread dump will print the log, and in windows you can use "CTRL+Break". Very simple and professional thread interview question, but if he asks you how to analyze it, it can be tricky.

11) why do we execute the run() method when we call the start() method, and why can't we directly call the run() method?

This is another very classic java multithreaded interview question. This is also my confusion when I started to write threaded programs. Now this question is usually asked in a phone interview or in the first round of the junior and intermediate Java interview. The answer to this question should be that when you call the start() method you will create a new thread and execute the code in the run() method. But if you call the run() method directly, it won't create a new thread or execute the code that calls the thread. Read my previous article on the difference between start and run methods for more information.

12) how do you wake up a blocked thread in Java?

This is a tricky problem with threads and blocks, and it has many solutions. If a thread encounters an IO block, I also don't think there is a way to abort the thread. If a thread is blocked by calling the wait(), sleep(), or join() methods, you can interrupt the thread and wake it up by throwing InterruptedException. My previous How to deal with blocking methods in java has a lot of information about dealing with thread blocking.

13) what is the difference between CycliBarriar and CountdownLatch in Java?

This thread problem is mainly used to check if you are familiar with JDK5 and send the package. The difference between the two is that CyclicBarrier can reuse the obstacles it has already passed, while CountdownLatch cannot.

14) what is an immutable object and how does it help to write concurrent applications?

Another multithreaded classic interview question, not directly related to the thread, but indirectly help a lot. This java interview question can be tricky if he asks you to write an immutable object, or asks you why String is immutable.

15) what are some common problems you encounter in multithreaded environments? How did you solve it?

Common examples of multithreaded and concurrent programs are Memory-interface, race conditions, deadlocks, live locks, and starvation. The problem is endless, and if you get it wrong, it will be hard to find and debug. This is mostly an interview-based rather than a practical application based Java thread issue.

A few additional questions:

1) what is the difference between a green thread and a local thread in java?

2) what is the difference between a thread and a process?

3) what is context switching in multithreading?

4) the difference between deadlocks and live locks, between deadlocks and starvation?

5) what is the thread scheduling algorithm used in Java?

6) what is thread scheduling in Java?

7) how do you handle uncaught exceptions in threads?

8) what is a thread group and why is it not recommended in Java?

9) why is it better to use the Executor framework than to create and manage threads using applications?

10) what is the difference between Executor and Executors in Java?

11) how do I find which thread USES CPU the longest on Windows and Linux?

Related articles: