Multithreading and concurrency issues are an essential part of any Java interview. If you want to get the front desk information for any stock investment bank, you should prepare a lot of questions about multithreading. Multithreading and concurrency are a very popular topic in the investment banking business, especially in the development of electronic transactions. They will ask the interviewer many confusing Java threading issues. The interviewer just wants to make sure that the interviewer has enough knowledge of Java threading and concurrency, because many of the candidates are floating on the surface.
- Now there are T1, T2, T3 three threads, how do you ensure that T2 execution after T1 execution, T3 after the T2 execution?
This thread problem is usually asked during the first or phone interview phase to detect whether you are familiar with the join method. This multithreading problem is relatively simple and can be implemented using the Join method.
- What is the advantage of the lock interface over the synchronized block in Java? You need to implement an efficient cache that allows multiple users to read, but only one user is allowed to write in order to maintain its integrity, how will you implement it?
The biggest advantage of the lock interface in multi-threaded and concurrent programming is that they provide a lock for both read and write, which satisfies high-performance data structures and conditional blocking that you write like Concurrenthashmap. The question of a Java thread interview is increasingly based on the interviewer's answer. I strongly recommend that you read locks 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 final.
- What is the difference between the wait and sleep methods in Java?
Frequently asked questions about a Java thread interview in a phone interview. The biggest difference is that wait waits for the lock to be released while sleep holds the lock. Wait is typically used for inter-thread interaction, and sleep is typically used to pause execution.
- Block queues with Java implementations.
This is a relatively difficult multi-threaded interview problem, it can achieve a lot of purposes. First, it can detect whether the candidate can actually write programs with Java threads, and second, can detect the candidate's understanding of the concurrency scenario, and you can ask many questions according to this. If he uses the Wait () and notify () methods to implement the blocking queue, you can ask him to write again with the concurrency class in the latest Java 5.
- Write code in Java to solve producer-consumer problems.
Similar to the question above, but the problem is more classic, sometimes the interview will ask the following questions. How to solve the producer-consumer problem in Java, of course there are a lot of solutions, I have shared a blocking queue implementation method. Sometimes they even ask how to make the Philosopher's meal a problem.
- What will you do with Java programming a program that will lead to deadlocks?
This is my favorite Java thread interview problem, because even deadlock problems are common when writing multithreaded concurrent programs, but many candidates are not able to write deadlock free code (no deadlock codes?). ), they struggled. 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 here the n can be replaced with 2, and the larger the data will make the problem look more complicated. Get more information about deadlocks by avoiding deadlocks in Java.
- What is atomic operations and what are atomic operations in Java?
A very simple Java thread interview problem, the next problem is that you need to synchronize an atomic operation.
- What is the key to volatile in Java? How to use it? How does it differ from the synchronized method in Java?
Thread issues based on volatile keywords have become more prevalent since the Java 5 and Java memory models have changed. Be prepared to answer questions about how volatile variables ensure visibility, sequencing, and consistency in a concurrency environment.
- What is the competitive condition? How do you find and solve competition?
This is an issue that appears in the advanced stages of a multithreaded interview. Most interviewers will ask about the competitive conditions you've met recently and how you've solved them. Some time they will write simple code, and then let you detect the code's competitive conditions. You can refer to the article I published earlier about the Java competition criteria. In my opinion this is one of the best Java thread interview questions that can be used to accurately detect candidates ' experience in solving competitive 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".
- 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 you can use "Ctrl+break" in Windows. Very simple and professional thread interview questions, but if he asks you how to analyze it, it will be tricky.
- Why does the run () method execute when we call the start () method, and why can't we call the Run () method directly?
This is another very classic Java multithreading interview problem. It was also a puzzle when I first started writing a thread. Now this question is usually asked in the first round of a phone interview or in the middle of a junior 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 does not create a new thread and does not execute code that invokes the thread. Read the "Start and run method differences" Article I wrote earlier to get more information.
- How do you wake up a blocked thread in Java?
This is a tricky problem with threading and blocking, and it has a lot of workarounds. If the thread encounters IO blocking, I do not think there is a way to abort the thread. If the thread is blocking because of a call to wait (), sleep (), or the Join () method, you can break thread and wake it up by throwing interruptedexception. I've written earlier how to deal with blocking methods in Java has a lot of information about handling thread blocking.
- What is the difference between Cyclibarriar and Countdownlatch in Java?
This threading problem is primarily used to detect if you are familiar with the concurrent packages in JDK5. The difference between the two is that cyclicbarrier can reuse the barriers that have been passed, while Countdownlatch cannot be reused.
- What is an immutable object and how does it help with writing concurrent applications?
Another multithreaded classic interview problem is not directly related to threads, but indirectly helps a lot. This Java interview problem can be tricky, if he asks you to write an immutable object, or asks you why the string is immutable.
- What are the common problems you encounter in a multithreaded environment? How did you deal with it?
Memory-interface, race conditions, deadlocks, live locks, and starvation are often encountered in multithreaded and concurrent programs. The problem is endless, and if you get it wrong, it will be hard to find and debug. This is most of the interview-based, not the actual application-based Java threading problem.
A few additional questions to add:
1) What is the difference between a green thread and a local thread in Java?
2) What is the difference between threads and processes?
3) What is context switching in multi-threading?
4) What is the difference between a deadlock and a live lock, 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 non-catching exceptions in the 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 create and manage threads with apps?
10) What is the difference between executor and executors in Java?
11) How do I find which thread is using the longest CPU time on Windows and Linux?
15 Top Java multithreaded questions and answers