Multithreading and concurrency issues are an essential part of any Java interview. If you want to get a front-end information position in any stock investment bank, then you should prepare a lot of questions about multithreading. Multithreading and concurrency are a very popular topic in investment banking, especially related to the development of electronic transactions. They will ask interviewers many confusing Java threading questions. The interviewer just wants to be sure that the interviewer has enough knowledge of Java threading and concurrency, because many of the candidates are only superficial. High capacity and low latency electronic trading systems for direct market-oriented transactions are inherently concurrent. The following are Java threading questions I like to ask at different times and places. I didn't provide an answer, but I'll give you clues whenever possible, and sometimes these clues are enough to answer questions. Now there are more and more problems referring to Java5 concurrent packages regarding concurrent tools and concurrent collections. ThreadLocal, Blocking Queue, Counting Semaphore and ConcurrentHashMap are more popular among those questions.
15 Java multi-threaded interview questions and answers
1) There are now 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 during the first round or telephone interview stage, with the purpose of checking 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 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 to write to maintain its integrity. How would you implement it?
The biggest advantage of lock interfaces in multi-threaded and concurrent programming is that they provide locks for read and write separately, which can satisfy you to write high-performance data structures like ConcurrentHashMap and conditional blocking. Java thread interview questions are increasingly asked based on the interviewer's answers. I highly recommend reading Locks carefully before you go to a multi-threaded interview, because it is currently used to build electronic transaction system client cache and transaction connection space.
3) What are the differences between wait and sleep methods in java?
Java thread interview questions that are often asked during phone interviews. The biggest difference is that wait releases the lock while waiting, while sleep always holds the lock. Wait is usually used for inter-thread interactions, and sleep is usually used to pause execution.
4) Use Java to implement blocking queues.
This is a relatively difficult multi-threaded interview question, which can achieve many goals. First, it can detect whether the candidate can actually write programs in Java threads; 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 write it again with the latest concurrent classes in Java 5.
5) Use Java to write code to solve the producer-consumer problem.
It is very similar to the above question, but this question is more classic. Sometimes the following questions will be asked in interviews. How to solve the producer-consumer problem in Java? Of course, there are many solutions. I have shared a method that uses blocking queues to implement. Sometimes they even ask how to realize the problem of philosophers eating.
6) How will you solve the problem of programming a program that will cause deadlock in Java?
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 (no deadlock code?), and they struggle. Just tell them that you have N resources and N threads, and you need all the resources to complete an operation. For simplicity, n can be replaced by 2, the larger the data will make the problem appear more complicated. Get more information about deadlocks by avoiding deadlocks in Java.
7) What is atomic operation and 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 key role of volatile in Java? How to use it? How is it different from the synchronized method in Java?
Since the Java 5 and Java memory models have changed, threading problems based on the volatile keyword have become increasingly popular. You should be ready to answer about how volatile variables ensure visibility in a concurrent environment.
9) What are competition conditions? How do you discover and solve competition?
This is a question that appears at the advanced stage of multi-threaded interviews. Most interviewers will ask about the competition conditions you have encountered recently and how you solved it. There are times when they write simple code and then let you detect the race conditions for the code. You can refer to my previous article about Java race conditions. In my opinion this is one of the best java thread interview questions, which can accurately detect candidates' experience in solving race conditions, or writing code which is free of data race or any other race condition. The best book about 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 question, but it will be tricky if he asks you how to analyze it.
11) Why do we execute the run() method when we call the start() method, and why can't we directly call the run() method?
This is another very classic Java multi-threaded interview question. This is also my confusion when I first started writing threading programs. Now this question is usually asked during the phone interview or the first round of junior and middle 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. But if you call the run() method directly, it will not create a new thread or execute the code that calls the thread. Read the article "The Difference between Start and Run Method" I wrote before to get more information.
12) How do you wake up a blocking thread in Java?
This is a tricky question about threading and blocking, and it has many solutions. If the thread encounters IO blockage, I don't think there is a way to abort the thread. If the thread blocks due to calling wait(), sleep(), or join() methods, you can interrupt the thread and wake it up by throwing an InterruptedException. The "How to deal with blocking methods in java" I wrote before has a lot of information about dealing with thread blocking.
13) What is the difference between CycliBarriar and CountdownLatch in Java?
This threading problem is mainly used to detect whether you are familiar with concurrent packages in JDK5. The difference between these two is that CyclicBarrier can reuse the barrier that has been passed, while CountdownLatch cannot be reused.
14) What is an immutable object and how does it help writing concurrent applications?
Another classic interview question for multi-threading is not directly related to threads, but it helps a lot indirectly. This java interview question can become 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 a multi-threaded environment? How did you solve it?
Commonly encountered in multithreaded and concurrent programs are Memory-interface, race conditions, deadlocks, live locks and hunger. The problem has no end, and if you get it wrong, it will be difficult to discover and debug. This is mostly interview-based, not practical application-based Java threading questions.
Several other questions added:
1) What is the difference between green thread and local thread in java?
2) The difference between threads and processes?
3) What is context switching in multithreading?
4) The difference between deadlock and live lock, 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 deal with uncatchable 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 applications?
10) What is the difference between Executor and Executors in Java?
11) How to find which thread uses the longest CPU time on Windows and Linux?
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.