15 Top Multithreaded Interview Questions

Java Threading Interview Questions

Questions about multithreading and concurrency are an essential part of any Java interview. If you want to get a front office information position in any stock investment bank, then you should prepare a lot of questions about multithreading. Multithreading and concurrency is a very popular topic in investment banking, especially as it relates to the development of electronic transactions. They ask interviewers a lot of confusing Java threading questions. The interviewer just wants to make sure that the interviewee has sufficient knowledge of Java threads and concurrency, since many of the candidates are only superficial. High-volume and low-latency electronic trading systems for direct-to-market transactions are inherently concurrent. Below are some Java thread questions that I like to ask at different times and places. I don't provide an answer, but I'll give you clues whenever possible, and sometimes those clues are enough to answer the question. There are now growing questions about concurrency tools and concurrent collections citing the Java 5 concurrency package. Of those problems, ThreadLocal, Blocking Queue, Counting Semaphore, and ConcurrentHashMap are more popular.

 

15 Java Multithreading Interview Questions and Answers

 

1) Now there are three threads T1, T2, T3, how do you ensure that T2 is executed after T1 is executed, and T3 is executed after T2 is executed?

 

This threading question is usually asked during the first round or phone interview to test your familiarity with the "join" method. This multi-threading problem is relatively simple and can be implemented with 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, in order 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 reading and writing, respectively, which can satisfy you when writing high-performance data structures like ConcurrentHashMap and conditional blocking. Java thread interview questions are increasingly being asked based on the interviewee's answers. I highly recommend reading Locks carefully before you go for a multithreaded interview, as it currently has a lot of client cache and transaction connection space used to build electronic transaction systems.

 

3) What is the difference between wait and sleep methods in java?

 

Java threading interview questions that are often asked during phone interviews. The biggest difference is that wait releases the lock while waiting, while sleep keeps holding the lock. Wait is usually used for inter-thread interaction, and sleep is usually used to suspend execution.

 

4) Implement blocking queue in Java.

 

This is a relatively tough multithreaded interview question, and it serves a lot of purposes. First, it can detect whether the candidate can actually write programs in Java threads; second, it can detect the candidate's understanding of the concurrency scenario, and you can ask many questions based on this. If he implements the blocking queue with wait() and notify() methods, you can ask him to write it again with the latest Java 5 concurrency classes.

 

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

 

Similar to the question above, but this one is more classic, and is sometimes asked in interviews. How to solve the producer-consumer problem in Java, of course there are many solutions, I have shared a method implemented by blocking queue. Sometimes they even ask how to implement the Philosopher's meal.

 

6) Programming a program in Java that will cause a deadlock, how would you solve it?

 

This is my favorite Java threading interview question because even though deadlock problems are very common when writing multithreaded concurrent programs, many candidates can't write deadlock free code (deadlock free code?) and they struggle. Just tell them that you have N resources and N threads, and you need all of them to complete an operation. For simplicity, n can be replaced by 2, larger data will make the problem look more complicated. Learn more about deadlocks by Avoiding Deadlocks in Java.

 

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

 

Very simple java threading interview question, the next question is that you need to synchronize an atomic operation.

 

8) What does the volatile key in Java do? How to use it? How is it different from the synchronized method in Java?

 

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

 

9) What is a race condition? How do you spot and resolve competition?

 

This is a question that comes up in the advanced stages of a multithreaded interview. Most interviewers will ask about a recent race condition you encountered and how you resolved it. Sometimes they write simple code and let you detect race conditions in your code. You can refer to my previous article on Java race conditions. This is one of the best java threading interview questions in my opinion, it can test the candidate's experience in solving race condition, 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.

 

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

 

In UNIX you can use kill -3 and thread dump will print the log, in windows you can use "CTRL+Break". Very simple and professional thread interview question, but tricky if he asks you how to analyze it.

 

11) 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 question. This is also the confusion when I first started writing thread programs. Now this question is usually asked in phone interviews or in the first round of junior-intermediate Java interviews. The answer to this question should be 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 nor execute the calling thread's code. Read my previous article "Difference between 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 the thread if it encounters IO blocking. If the thread is blocked by calling wait(), sleep(), or join(), you can interrupt the thread and wake it up by throwing an InterruptedException. "How to deal with blocking methods in java" I wrote earlier has a lot of information on dealing with thread blocking.

 

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

 

This threading question is mainly used to check if you are familiar with the concurrency package in JDK5. The difference between these two is that CyclicBarrier can reuse already passed barriers, while CountdownLatch cannot.

 

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

 

Another classic multi-threaded interview question, not directly related to threads, but indirectly helpful a lot. This java interview question can get really 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 multithreaded environment? How did you solve it?

 

Commonly encountered in multi-threaded and concurrent programs are Memory-interface, race conditions, deadlocks, livelocks, and starvation. The problems are endless, and if you get it wrong, it will be hard to spot and debug. This is mostly interview based, not practical application based Java threading questions.

 

Additional questions:

 

1) What is the difference between green thread and native thread in java?

 

2) What is the difference between thread and process?

 

3) What is context switching in multithreading?

 

4) What is the difference between deadlock and livelock, and the difference between deadlock and pie?

 

5) What is the thread scheduling algorithm used in Java?

 

6) What is thread scheduling in Java?

 

7) How do you handle uncatchable exceptions in threads?

 

8) What is a thread group and why is it deprecated in Java?

 

9) Why is it better to use the Executor framework than to use the application to create and manage threads?

 

10) Difference between Executor and Executors in Java?

 

11) How to find which thread is using the most CPU time on Windows and Linux?

Guess you like

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