15 top-level Java multi-thread Interview Questions and Answers, multi-thread questions and answers
Multithreading and concurrency issues are an essential part of any Java interview. If you want to obtain information about any stock and investment banks, you should prepare a lot of questions about multithreading. Multithreading and concurrency in investment banking are a very popular topic, especially related to the development of electronic transactions. They will ask the interviewer many confusing Java thread questions. The interviewer just wants to be sure that the interviewer has enough knowledge about Java threads and concurrency, because many candidates are superficial.
1) now there are three threads T1, T2, and T3. How do you ensure that T2 is executed after T1 execution and T3 is executed after T2 execution?
This thread is usually asked during the first round or telephone interview to check if you are familiar with the join method. This multithreading problem is relatively simple and can be implemented using the join method.
2) in Java, what are the advantages of the Lock interface over the synchronized block? You need to implement an efficient cache, which allows multiple users to read but only allows one user to write to maintain its integrity. How do you implement it?
The biggest advantage of the lock interface in multi-thread and concurrent programming is that they provide locks for read and write respectively, which can meet your needs to write high-performance data structures such as ConcurrentHashMap and implement conditional blocking. Questions about Java thread interviews are increasingly answered by the interviewer. I strongly recommend that you carefully read Locks before attending a multi-thread interview, because it is currently used to build a large number of client caches and transaction connection spaces for the final electronic transaction system.
3) What are the differences between the wait and sleep methods in java?
Java thread interview questions that are frequently asked during telephone interviews. The biggest difference is that wait releases the lock while waiting, and sleep keeps holding the lock. Wait is usually used for interaction between threads, and sleep is usually used to suspend execution.
4) use Java to implement blocking queues.
This is a relatively difficult multi-thread interview problem, which can achieve many goals. First, it can check whether the selector can actually write a program using a Java thread; second, it can check the selector's understanding of the concurrent scenario, and you can ask a lot of questions based on this question. If you use the wait () and notify () methods to implement blocking queues, you can ask him to write it again using the concurrent class in the latest Java 5.
5) use Java to write code to solve the producer-consumer problem.
This is similar to the above question, but it is more classic. In some cases, the following questions will be asked during the interview. In Java, there are many solutions to the producer-consumer problem. I have already shared a method to implement blocking queues. In some cases, they may even ask how to achieve the dining of philosophers.
6) How do you solve the deadlock caused by programming a program in Java?
This is my favorite Java thread interview question, because even if the deadlock problem is very common when writing multi-threaded concurrent programs, many candidates cannot write the deadlock free code (no deadlock code ?), They are struggling. Just tell them that you have N resources and N threads, and you need all resources to complete an operation. In order to make it simple, Here n can be replaced with 2, the larger the data will make the problem look more complicated. You can get more information about deadlocks by avoiding deadlocks in Java.
7) What are atomic operations and what are atomic operations in Java?
This is 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? In Java, what is the difference between it and the synchronized method?
Since the Java 5 and Java memory models have changed, thread issues based on the volatile keyword have become increasingly popular. You should be prepared to answer questions about how volatile variables ensure visibility, order, and consistency in the concurrency environment.
9) What are the conditions for competition? How do you find and solve the competition?
This is a problem that occurs in the advanced stage of multi-thread interviews. Most interviewers will ask about your recent competitive conditions and how you solve them. Some time they will write simple code and then let you detect the competition conditions of the Code. Refer to my previous article on Java competition conditions. In my opinion, this is one of the best java thread interview questions. It can precisely detect the candidate's experience in solving the competition conditions, or writing code which is free of data race or any other race condition. The best book in this regard 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 thread dump will print logs. 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 tricky.
11) Why can't we directly call the run () method when we call the start () method?
This is another classic java multi-thread interview question. This is also the confusion when I first started writing a thread program. This question is usually asked by phone or in the first round of the preliminary and intermediate Java interview. The answer to this question should be like this. When you call the start () method, you will create a new thread and execute the code in the run () method. However, if you call the run () method directly, it does not create a new thread or execute the code that calls the thread. Read the article "differences between the start and run methods" I wrote to get more information.
12) How do you wake up a blocked thread in Java?
This is a tricky question 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 the wait (), sleep (), or join () method, you can interrupt the thread and throw InterruptedException to wake it up. I wrote a lot about How to deal with blocking methods in java.
13) What is the difference between CycliBarriar and CountdownLatch in Java?
This thread problem is mainly used to check whether you are familiar with and send packets in JDK 5. The difference between the two is that javasicbarrier can reuse the obstacles that have been passed, while CountdownLatch cannot be reused.
14) What is an immutable object and how does it help write concurrent applications?
Another typical multi-thread interview question is not directly related to threads, but it is helpful indirectly. This java interview question can be very tricky. If he asks you to write an immutable object or ask you why the String is immutable.
15) What are the common problems you encounter in a multi-threaded environment? How did you solve it?
Memory-interface, competitive conditions, deadlocks, live locks, and hunger are often encountered in multithreading and concurrent programs. There is no end to the problem. If you make a mistake, it will be difficult to find and debug it. This is mostly based on interviews, rather than Java thread issues based on actual applications.Other questions added:
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) What is the difference between a deadlock and a live lock? What is the difference between a deadlock and a pie?
5) What is the thread scheduling algorithm used in Java?
6) What is Thread Scheduling in Java?
7) How do you handle exceptions that cannot be captured in the thread?
8) What is a thread group? Why is it not recommended in Java?
9) Why is it better to use the Executor framework than to use application creation and management threads?
10) What is the difference between Executor and Executors in Java?
11) on Windows and Linux, how does one find which thread has the longest CPU usage time?
Java study and exchange QQ group: 589809992 chat prohibited, do not enter!