Home  >  Article  >  Java  >  15 Advanced Java Multithreading Interview Questions and Answers

15 Advanced Java Multithreading Interview Questions and Answers

高洛峰
高洛峰Original
2017-01-16 16:09:161649browse

Java Threading Interview Questions

Questions about multi-threading and concurrency are an essential part of any Java interview. If you want to get a front-office information position at any stock investment bank, then you should be prepared with a lot of questions about multi-threading. Multithreading and concurrency is a very popular topic in investment banking, especially as it relates to the development of electronic trading. They ask interviewers a lot of confusing Java threading questions. The interviewer just wants to make sure that the interviewer has enough knowledge about Java threads and concurrency, because many candidates only scratch the surface. High-volume and low-latency electronic trading systems for direct-to-market trading are inherently concurrent. The following are Java threading questions that I like to ask at different times and places. I don't provide answers, but whenever possible I give you clues, and sometimes those clues are enough to answer the question. There is an increasing number of questions regarding concurrency tools and concurrent collections referencing the Java 5 Concurrency Package. Among those problems, ThreadLocal, Blocking Queue, Counting Semaphore and ConcurrentHashMap are more popular.


15 Java multi-threading interview questions and answers

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

This thread question is usually asked in the first round or phone interview stage to test whether you are familiar with the "join" method. This multi-threading problem is relatively simple and can be implemented using the join method.

2) What are the advantages of Lock interface over synchronized block in Java? You need to implement an efficient cache that allows multiple users to read, but allows only one user to write, thereby maintaining its integrity. How would you implement this?

The biggest advantage of the lock interface in multi-threaded and concurrent programming is that they provide separate locks for reading and writing, which allows you to write high-performance data structures like ConcurrentHashMap and conditional blocking. Java thread interview questions are increasingly based on the interviewer's answers. I strongly recommend reading up on Locks before you go for a multi-threaded interview, as it is currently heavily used in client-side caching and trading connection space for building electronic trading terminals.

3) What is the difference between wait and sleep methods in java?

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

4) Use Java to implement blocking queue.

This is a relatively difficult multi-threaded interview question that can achieve many purposes. First, it can test whether the candidate can actually write programs using Java threads; second, it can test the candidate's understanding of concurrency scenarios, and you can ask many 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 using the latest concurrency classes in Java 5.

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

is very similar to the above question, but this question is more classic. Sometimes interviews will ask the following question. How to solve the producer-consumer problem in Java? Of course there are many solutions. I have shared a method that is implemented using a blocking queue. Sometimes they even ask how to implement the dining philosophers problem.

6) Use Java to program a program that will cause deadlock. How will you solve it?

This is my favorite Java thread interview question, because even though deadlock problems are very common when writing multi-threaded concurrent programs, many candidates cannot write deadlock free code (deadlock-free code?) , they struggled. 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. Larger data will make the problem look more complicated. Get more information about deadlocks by Avoiding Deadlocks in Java.

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

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

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

Since Java 5 and the Java memory model changes, threading issues based on the volatile keyword have become more and more popular. You should be prepared to answer questions about how volatile variables ensure visibility, ordering, and consistency in a concurrent environment.

9) What is a race condition? How do you identify and resolve competition?

This is a question that appears in the advanced stages of multi-threaded interviews. Most interviewers will ask about a competitive condition you encountered recently and how you resolved it. Sometimes they will write simple code and let you detect race conditions in the code. You can refer to my previous article on Java race conditions. In my opinion this is one of the best java thread interview questions, it can accurately detect the candidate's experience in solving race conditions, or writing code which is free of data race or any other race condition (this sentence which is free of data race will not translate). The best book on this subject is "Concurrency practices in Java".

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

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

11) Why does the run() method execute when we call the start() method? Why can’t we call the run() method directly?

This is another very classic java multi-threading interview question. This was also my confusion when I first started writing thread programs. Nowadays this question is usually asked in phone interviews or in the first round of beginner to intermediate level Java interviews. The answer to this question should be this, 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 and will not 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 tough question about threads and blocking, and it has many solutions. I don't think there is a way to abort the thread if it encounters an IO block. If a thread is blocked by calling wait(), sleep(), or join(), you can interrupt the thread and wake it up by throwing InterruptedException. My previous article "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 question is mainly used to test whether you are familiar with the concurrent package in JDK5. The difference between the two is that CyclicBarrier can reuse barriers that have been passed, while CountdownLatch cannot be reused.

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

Another classic multi-threading interview question, it is not directly related to threads, but it indirectly helps a lot. This Java interview question can get very tricky 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 multi-threaded environments? How did you solve it?

Commonly encountered in multi-threaded and concurrent programs are Memory-interface, race conditions, deadlock, livelock and starvation. The problems are endless, and if you get it wrong, it will be difficult to find and debug. This is mostly interview based and not practical application based Java threading questions.

A few additional questions:

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

2) What is the difference between thread and process?

3) What is context switching in multi-threading?

4) What is the difference between deadlock and livelock, and the difference between deadlock and pie?

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

6) What is thread scheduling in Java?

7) How do you handle uncatchable 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 use the application to create and manage threads?

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

11) How to find which thread is using the most CPU time on Windows and Linux?

For more 15 advanced Java multi-threading interview questions and answers, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn