Hematemesis and sort out a wave of 2020 concurrent programming interview questions, these are all must-learn knowledge points!

Basic probability

The so-called concurrent programming refers to the "simultaneous" processing of multiple tasks on a single processor. Concurrency is multiple events on the same entity. Multiple events occur at the same time interval.

Theme

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 T2 is executed?
This thread question is usually asked in the first round or the phone interview stage, the purpose is to test whether you are familiar with the "join" method. This multi-threading problem is relatively simple and can be implemented with the join method.

2. What are the advantages of the Lock interface over the synchronized block in Java?
You need to implement an efficient cache, which allows multiple users to read, but only allows one user to write, in order to maintain its integrity. How would you implement it? The biggest advantage of the lock interface in multithreading and concurrent programming is that they provide locks for reading and writing respectively. It can satisfy you for writing high-performance data structures like ConcurrentHashMap and conditional blocking. Java thread interview questions will increasingly be asked based on the interviewer's answers. I strongly recommend that you read Locks carefully before you go to the multi-threaded interview, because it is currently a large number of client-side caches and transaction connection spaces used to build electronic transaction systems.

3. The difference between wait and sleep methods in java?
Java thread interview questions that are often asked in telephone 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 suspend execution.

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

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 interview will ask the following questions. How to solve the producer-consumer problem in Java, of course, there are many solutions, I have shared a method to achieve a blocking queue. Sometimes they even ask how to realize the philosopher's meal problem.

6. Using Java to program a program that will cause a deadlock, how would you solve it?
This is my favorite Java thread interview question, because even though the deadlock problem is very common when writing multi-threaded concurrent programs, many candidates cannot write deadlock free code (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 can be replaced with 2. The larger the data, the more complicated the problem will be. Get more information about deadlocks by avoiding deadlocks in Java.

7. What are atomic operations, and what are atomic operations in Java?
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 Java 5 and the Java memory model changed, 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 in a concurrent environment.

9. What are race conditions? How do you discover and resolve competition?
This is a question that appears at the advanced stage of multithreaded interviews. Most interviewers will ask about the competitive conditions you encountered recently and how you resolved them. Sometimes they will write simple code and then 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. The best book on this is "Concurrency practices in Java".

10. How will you use threaddump?
How will you analyze Thread dump? In UNIX, you can use kill -3, and thread dump will print the log. In Windows, you can use "CTRL+Break". Very simple and professional threaded interview question, but if he asks you how to analyze it, it will be tricky.

11. Why do we execute the run() method when we call the start() method, why can’t we call the run() method directly?
This is another very classic java multithreaded interview question. This is also the confusion when I first started writing thread programs. Now this question is usually asked in the phone interview or in the first round of the middle-level Java interview. 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 or execute the code of the calling thread. Read the article I wrote earlier "The difference between start and run methods" for more information.

12. How do you wake up a blocked thread in Java?
This is a thorny problem about threads and blocking, and 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. The "How to deal with blocking methods in java" I wrote earlier has a lot of information about handling thread blocking.

13. What is the difference between CycliBarriar and CountdownLatch in Java?
This thread question is mainly used to check whether you are familiar with concurrent packages in JDK5. The difference between the two is that CyclicBarrier can reuse barriers that have passed, while CountdownLatch cannot be reused.

14. What is an immutable object and how does it help to 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 be very tricky, if he asks you to write an immutable object, or ask you why String is immutable.

15. What are the common problems you encounter in a multithreaded environment?
How did you solve it? Commonly encountered in multithreaded and concurrent programs are Memory-interface, race conditions, deadlock, livelock and starvation. The problems are endless, and if you make a mistake, it will be difficult to find and debug. This is mostly based on interviews, not based on actual applications of Java threading questions.

Off topic

Due to too much information, this article is limited in space and only shared a small part of the information. If you need a full set of real questions (including answers to all questions), please click here. Code: qf
Recently, it is the best time to find a job. Everyone finds your favorite job smoothly!
Insert picture description here

Guess you like

Origin blog.csdn.net/w1103576/article/details/108625569
Recommended