Questions about multithreading and concurrency are a necessary part of any Java interview. If you want to get more positions, you should prepare lots of questions about multithreading.

They ask candidates a lot of confusing questions about Java threads. The interviewer just wants to make sure that the candidate has sufficient knowledge of Java threads and concurrency, because many candidates are just scratching the surface. The problems with concurrent tools and concurrent collections are increasing now that Java5 is referenced and packaged. Among those problems, ThreadLocal, Blocking Queue, Counting Semaphore, and ConcurrentHashMap are popular.

Open baidu APP to see hd pictures

Java Multithreading interview questions and answers

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

This thread question is usually asked during the first round or phone interview to test your familiarity with the “join” method. This multithreading problem is relatively simple and can be implemented using the Join method.

  1. What are the advantages of Lock interfaces over synchronized blocks in Java? You need to implement an efficient cache that allows multiple users to read, but only one user to write, in order to maintain its integrity. How would you implement that?

The biggest advantage of lock interfaces in multithreaded and concurrent programming is that they provide locks for reads and writes, which allow you to write high-performance data structures like ConcurrentHashMap and conditional blocking. More and more, Java thread interview questions will be based on the candidate’s answers. I highly recommend that you read Locks carefully before going to a multi-threaded interview, as it currently has a lot of client-side caching and transaction connection space used to build electronic transaction terminals.

  1. What is the difference between wait and sleep in Java?

Java thread interview questions that are often asked in phone interviews. The big difference is that while wait releases the lock, sleep holds it all the time. Wait is usually used for interthread interactions, and sleep is usually used to pause execution.

4) Implement blocking queues in Java.

This is a relatively difficult multi-threaded interview question that serves many purposes. First, it checks whether the candidate can actually write programs in Java threads. Second, you can test a candidate’s understanding of concurrent scenarios, and you can ask a lot of questions based on that. If he uses wait() and notify() to implement blocking queues, you can ask him to do it again with the latest Java 5 concurrency classes.

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

This is a very similar question to the one above, but this is a more classic one that is sometimes asked in an interview. There are, of course, many solutions to the producer-consumer problem in Java, and I’ve shared one that uses blocking queues. Sometimes they even ask how the philosopher can eat.

6) How would you solve a deadlock problem in Java?

This is my favorite Java thread interview question, because even though deadlock problems are common when writing multithreaded concurrent programs, many candidates cannot write 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. We can replace this n with 2 for simplicity, and the larger the number, the more complicated it looks. Learn more about deadlocks by avoiding them in Java.

  1. 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.

  1. What is the key to volatile in Java? How to use it? How is it different from the synchronized method in Java?

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

  1. What are the competitive conditions? How do you find and solve competition?

This is a question that comes up in the advanced stages of multi-threaded interviews. Most interviewers will ask about recent competitive situations and how you handled them. Sometimes they write simple code and ask you to check out the race conditions of the code. Refer to my previous post on Java competition conditions. In my opinion, this is one of the best Java thread interview questions to test for certainty the candidate’s experience in dealing with competitive conditions, or writing code which is free of data race or any other race condition. The best book on this subject is Concurrency Practices in Java.

  1. How would you use Thread dump? How would you analyze Thread dump?

On UNIX you can use kill -3 and Thread dump will print logs. On Windows you can use CTRL+Break. Very simple and professional thread interview question, but if he asks you how to analyze it, it can be tricky.

  1. Why do we call the run() method when we call the start() method, and why can’t we call the run() method directly?

This is another classic Java multithreaded interview question. This is the same puzzle I had when I first started writing threaded programs. Now this question is usually asked in a phone interview or in the first round of an intermediate Java interview. The answer to this question is 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 doesn’t create a new thread or execute the code that calls the thread. Read my previous article “The Difference between the Start and Run methods” for more information.

12) How do you wake up a blocked thread in Java?

This is a tricky problem with threads and blocking, and it has many solutions. I don’t think there is a way to abort a thread if it encounters IO blocking. 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 handling thread blocking.

13) What is the difference between CycliBarriar and CountdownLatch in Java?

This thread issue is mainly used to check if you are familiar with JDK5 and send packages. The difference between the two is that cyclicBarriers can be reused for already-passed barriers, while countdownlatches cannot.

  1. What are immutable objects, and how does it help write concurrent applications?

Another classic multi-threaded interview question, not directly related to threads, but indirectly helpful. This Java interview question can get tricky if he asks you to write an immutable object, or asks you why strings are immutable.

  1. What are the common problems you encounter in multi-threaded environments? How did you solve it?

Common examples of multithreaded and concurrent programs are memory-interfaces, race conditions, deadlocks, live locks, and starvation. The problem is endless, and if you get it wrong, it’s hard to find and debug. This is the most interview-based, not application-based Java thread question.

Forward attention + private letter xiaobian “learning” can get more Java learning materials oh!

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =