15 top Java multithreaded interview questions (with answers)

Multithreading and concurrency issues are an essential part of any Java interview. If you want to get a front desk information position in any stock investment bank, you should prepare a lot of questions about multithreading. Multithreading and concurrency are very popular topics in investment banking, especially in the development of electronic transactions. They ask interviewers a lot of confusing Java threading questions. The interviewer just wants to make sure that the interviewer has enough knowledge of Java threading and concurrency, because many candidates are only superficial. The electronic trading system with high capacity and low delay for direct market-oriented trading is concurrent in nature. Here are the java thread questions I like to ask at different times and places. I don't provide answers, but I will give you clues whenever possible. Sometimes these clues are enough to answer questions. Now there are more and more problems referring to Java 5 and contracting about concurrent tools and concurrent collections. ThreadLocal, blocking queue, counting semaphore and concurrenthashmap are popular among those problems.

15 Java multithreaded interview questions and answers

1) Now there are three threads: T1, T2 and T3. How do you ensure that T2 executes after T1 and T3 executes after T2?

This thread question is usually asked in the first round or telephone interview to test whether you are familiar with the "join" method. This multithreading problem is relatively simple and can be implemented by the join method.

2) What are the advantages of lock interfaces over synchronized blocks in Java? You need to implement an efficient cache, which allows multiple users to read, but only one user to write, so as to maintain its integrity. How would you implement it?

The biggest advantage of lock interface in multithreading and concurrent programming is that they provide locks for reading and writing respectively. It can meet the requirements of writing high-performance data structures such as concurrenthashmap and conditional blocking. Java thread interview questions are increasingly based on the interviewer's answers. I strongly recommend that you read locks carefully before you go to a multi-threaded interview, because it currently has a lot of client cache and transaction connection space for building electronic transaction terminals.

3) What are the differences between wait and sleep methods in Java?

Java thread interview questions that are often asked in telephone interviews. The biggest difference is that while waiting, wait releases the lock, while sleep always holds the lock. Wait is usually used for inter thread interaction, and sleep is usually used to pause execution.

4) Blocking queue is implemented in Java.

This is a relatively difficult multi-threaded interview question, which can achieve many purposes. First, it can detect whether candidates can actually write programs with Java threads; Second, you can test the candidate's understanding of concurrency scenarios, and you can ask a lot of questions based on this. If he uses the wait () and notify () methods to implement the blocking queue, you can ask him to write it again with the latest concurrency class in Java 5.

5) Write code in Java to solve the producer consumer problem.

It is similar to the above question, but this question is more classic. Sometimes the following questions will be asked in the interview. How to solve the producer consumer problem in Java, of course, there are many solutions. I have shared a method implemented with blocking queue. Sometimes they even ask how to realize the philosopher's meal.

6) Using java to program a program that will cause deadlock, how will you solve it?

This is my favorite java thread interview question, because even if deadlock is very common when writing multithreaded concurrent programs, But many candidates cannot write deadlock free code (no deadlock 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 simplicity, n here can be replaced with 2, and the larger the data will make the problem look more complex. Get more information about deadlock by avoiding deadlock in Java.

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

A very simple java thread interview question. The next question is that you need to synchronize an atomic operation.

8) What is the key role of volatile in Java? How to use it? How is it different from the synchronized method in Java?

Since the changes of Java 5 and JAVA memory model, threading based on volatile keyword has become more and more popular. You should be ready to answer questions about how volatile variables ensure visibility in a concurrent environment.

9) What are the competitive conditions? How do you find and solve the competition?

This is a question that appears in the advanced stage of multithreaded interview. Most interviewers will ask about the competitive conditions you have encountered recently and how you solved them. Sometimes they will write simple code and let you detect the competition conditions of the code. You can refer to my previous article on Java competitive conditions. In my opinion, this is one of the best java thread interview questions, or writing code which is free of data race or any other race condition. The best book on this is concurrency practices in Java.

10) How will you use thread dump? How will you analyze thread dump?

In UNIX, you can use kill - 3, and then thread dump will print the log. In windows, you can use "Ctrl + break". Very simple and professional thread interview questions, but if he asks you how to analyze it, it will be very difficult.

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

This is another classic java multithreaded interview question. This is also my confusion when I first started writing threaded programs. Now this question is usually asked in the first round of telephone interview or 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 will not create a new thread or execute the code of the calling thread. Read my previous article "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 about threads and blocking. It has many solutions. If the thread encounters IO blocking, I don't think there is a way to abort the thread. If the thread is blocked by calling wait (), sleep (), or join (), you can interrupt the thread and wake it up by throwing interruptedexception. How to deal with blocking methods in Java, which I wrote earlier, contains a lot of information about handling thread blocking.

13) What is the difference between cyclibariar and countdownlatch in Java?

This thread problem is mainly used to detect whether you are familiar with concurrent packages in jdk5. The difference between the two is that cyclicbarrier can reuse the obstacles that have passed, while countdownlatch cannot reuse them.

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

Another classic multi-threaded interview question is not directly related to threads, but it helps a lot indirectly. This Java interview question can become very difficult if he asks you to write an immutable object, or asks you why string is immutable.

15) What are the common problems you encounter in a multithreaded environment? How did you solve it?

Memory interface, race condition, deadlock, livelock and starvation are often encountered in multithreaded and concurrent programs. The problem is endless. If you make a mistake, it will be difficult to find and debug. This is most interview based, not application based, Java threading questions.

Additional questions:

1) What is the difference between green threads and local threads in Java?

2) What is the difference between threads and processes?

3) What is context switching in multithreading?

4) The difference between deadlock and livelock, the difference between deadlock and hunger?

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

6) What is thread scheduling in Java?

7) How do you handle non catching exceptions in a thread?

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 use applications to create and manage threads?

10) What is the difference between executors and executors in Java?

11) How do I find out which thread is using the longest CPU time on windows and Linux?

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>