15 Top Java Multithreading Interview Questions and Answers

Questions about multithreading and concurrency are an essential part of any java interview.

 

1) There are now three threads: T1, T2 and T3. How can I ensure that T2 is executed after T1 is completed and T3 is executed after T2 is completed?

This threading question is usually asked during the first phase or phone interview phase to determine if you are familiar with the "connect" method. This multithreading problem is simpler and can be implemented in the join method.

 

2) What is the advantage of lock interface over 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 did you achieve it?

The biggest advantage of lock interfaces in multi-threaded and concurrent programming is that they provide locks for reading and writing separately, which can satisfy high-performance data structures and conditions that prevent you from writing like ConcurrentHashMap. The interview questions of the java thread are becoming more and more based on the interviewer's answers. I strongly recommend that you read up on locks before doing multithreaded access, as it is now widely used to build client-side caches and transactional connection spaces for electronic transactions.

 

3) Is there a difference between await and sleep methods in java?

Interviews for java threads tend to be asked during phone interviews. The big difference is that while the wait is waiting, the lock is released, and sleep locks the lock. Waiting is typically used for inter-thread interaction, while sleep is typically used to suspend execution.

 

4) The blocking queue is implemented in java.

This is one of the more difficult multi-threaded interview questions, and it can achieve many goals. First, it can detect if the candidate can write programs with java threads. Second, it detects the candidate's understanding of concurrent scenarios and asks many questions based on that. If he implements blocking queue with wait() and notify() methods, you can ask him to write the latest java 5 concurrency.

 

5) The code written in java solves the producer-consumer problem.

This is very similar to the question above, but it's a more classic question, and the following question is sometimes asked in interviews. There are, of course, many solutions how to solve the java producer-consumer problem, and I've shared a blocking queue approach. Sometimes they even ask how to cook a philosopher's meal.

 

6) Programming programs, using java may lead to deadlocks, how will you solve them?

This is my favorite java thread interview question, because even when writing multi-threaded concurrent programs, which are very common deadlock problems, many candidates cannot write deadlock-free code (deadlock-free code). They are struggling. Just tell them you have N resources and N threads, and you need all of them to do an operation. To simply replace n with 2, bigger data makes the problem look more complicated. More information on deadlocks is done by avoiding deadlocks in java.

 

7) What is atomic operation, what is atomic operation in java?

A very simple java thread interview question, the next question is, you need to synchronize an atomic operation.

 

8) What is the key role of volatile in java? How to use it? What is the difference from synchronized method in java?

Due to changes in java 5 and the java memory model, threading issues based on the keyword volatile are getting more and more attention. You should be prepared to answer how you can ensure the visibility, sequencing, and consistency of volatile variables in a parallel environment.

 

9) What are the conditions of competition? How do you find and resolve competition?

This is a question that arises during the advanced stages of a multithreaded interview. Most interviewers are asking about the competitive environment you have faced recently and how you have addressed them. Sometimes they write simple code and then let you find race conditions in the code. You can refer to my previous article on race conditions in java. In my opinion this is one of the best java threading interview questions. It can test that candidate's experience to solve the exact same race condition, or writing, which is free data race or race code," "this is the best book 'java' parallel practice.

 

10) How to use thread dump? How would you analyze a thread dump?

In UNIX you can use kill-3, thread dump will print the log, in Windows you can use "Ctrl+Break". This is a 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, why can't we call the run() method directly?

This is a very classic java multithreading interview question. This was also a problem when I first started writing threaded programs. Now this question is usually asked in a phone interview or in the first round of a first intermediate java interview. The answer to this question should be that when you call the start() method, you create a new thread and execute the code in the run() method. However, if you call the run() method directly, it will not create a new thread and will not execute the calling thread's code. Read my previous article on 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 threading and blocking, and it has many solutions. If the thread is blocked by IO, I don't think there is a way to stop the thread. If the thread blocks by calling wait(), sleep(), or join(), you can interrupt the thread and wake it up by throwing an InterruptedException. How to handle blocking methods In java, which I wrote earlier, there is a lot of information about handling blocked threads.

 

13) What is the difference between CycliBarriar and countdownlatch in java?

The questions in this thread are mainly used to check if you are familiar with JDK5 concurrent packages. The difference between the two is that cyclicbarrier can reuse barriers that have passed and countdownlatch cannot.

 

14) What is an immutable object and how does it help in writing concurrent applications?

Another classic multi-threaded interview question that has nothing to do with threads, but it helps a lot. This java interview question is very tricky if he asks you to write an immutable object or asks you why strings are immutable.

 

15) What are the common problems encountered in a multithreaded environment? How did you solve it?

Frequently encountered multithreading and memory interfaces, craft complex race conditions, deadlocks, livelocks and starvation. There is no end to this question. If you make a mistake, it's hard to find and debug. This is the most basic interview, not a question of java threads, based on practical applications.

 

Follow our "java union" to get more technical dry goods:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325122556&siteId=291194637