15 advanced Java multi threaded interview questions and answers

  • 2020-04-01 03:22:50
  • OfStack

Java thread interview questions

Multithreading and concurrency issues are an essential part of any Java interview. If you want to get a front desk 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 in relation to the development of electronic trading. They ask a lot of confusing questions about Java threading. The interviewer just wants to make sure that the candidate has enough knowledge of Java threading and concurrency, because so many of the candidates are superficial. High-volume and low-latency electronic trading systems for direct-to-market transactions are inherently concurrent. Here are some of the Java threading questions I like to ask at different times and places. I'm not providing answers, but I'll give you clues whenever possible, and sometimes those clues are enough to answer the question. Problems with referencing Java5 and distributing packages about concurrent tools and concurrent collections are on the rise. ThreadLocal, Blocking Queue, Counting Semaphore, and ConcurrentHashMap are popular for those problems.


15 Java multi-threaded interview questions and answers

1) there are three threads: T1, T2 and T3. How do you ensure that T2 will execute after T1 and T3 will execute after T2?

This threading question is usually asked in the first round or during a phone interview to check your familiarity with the "join" method. This multithreading problem is relatively simple and can be implemented using the join method.

2) what are the advantages of the Lock interface over synchronized blocks 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 do you implement that?

The biggest advantage of the lock interfaces in multithreaded and concurrent programming is that they provide locks for reads and writes respectively, allowing you to write high-performance data structures like ConcurrentHashMap and conditional blocking. Questions for Java thread interviews are increasingly being asked based on the candidate's answers. 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 trade connection space for the electronic trading end.

3) the difference between the wait and sleep methods in Java?

The Java thread interview question that is often asked during phone interviews. The big difference is that while waiting, wait releases the lock, while sleep holds the lock. A Wait is usually used to interact between threads, and a sleep is usually used to pause execution.

4) implement blocking queues in Java.

This is a relatively tough multi-threaded interview question that can accomplish a lot. First, it detects whether candidates can actually write programs in Java threads. Second, you can test the candidate's understanding of the concurrency scenario, and you can ask a lot of questions based on that. If he implements a blocking queue using the wait() and notify() methods, you can ask him to write it again using the latest Java 5 concurrency class.

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

Similar to the above question, but this one is more classic. How to solve the producer-consumer problem in Java, of course, there are many solutions, and I've Shared one with blocking queues. Sometimes they even ask how to make the philosopher eat.

6) how do you solve a deadlock in Java?

This is my favorite Java threading interview question, because even though deadlock problems are common when writing multithreaded concurrent programs, many candidates can't write deadlock free code. They are struggling. Just tell them that you have N resources and N threads, and you need all of them to complete an operation. This n can be replaced by 2 for simplicity, and the larger the number, the more complicated the problem looks. Get more information about deadlocks by avoiding them in Java.

7) what are atomic operations and what are atomic operations in Java?

Very simple Java thread interview question, the next problem is that you need to synchronize an atomic operation.

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

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

9) what are the conditions of competition? How do you find and solve competition?

This is a question that comes up at the advanced stage of a multi-threaded interview. Most interviewers will ask you about recent competitive conditions and how you solved them. Sometimes they'll write simple code and ask you to detect the race conditions of the code. Refer to my previous post on Java race conditions. In my opinion, this is one of the best Java thread interview questions, which can exactly detect the candidate's experience in solving the competition condition, or writing code which is free of data race or any other race condition. The best book on 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 if he asks you how to analyze it, it can be tricky.

11) why do we execute the run() method when we call the start() method, and why can't we call the run() method directly?

This is another very classic Java multithreaded interview question. This is also my confusion 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 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 won't create a new thread or execute the code that calls the thread. Read my previous article the 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 blocks, and it has many solutions. If the thread encounters an IO block, I don't think there is a way to abort the thread. If a thread is blocked by calling the wait(), sleep(), or join() methods, you can interrupt the thread and wake it up by throwing InterruptedException. My previous book, How to deal with blocking methods in Java, has a lot of information about dealing with thread blocking.

13) what's the difference between CycliBarriar and CountdownLatch in Java?

This thread problem is mainly used to check if you are familiar with JDK5 and issue packets. The difference between the two is that CyclicBarrier can reuse passed barriers, while CountdownLatch cannot.

14) what is an immutable object and how does it help to write concurrent applications?

Another classic multi-threading interview question, not directly related to threads, but indirectly helps a lot. This Java interview question can be very tricky if it asks you to write an immutable object, or asks you why String is immutable.

15) what are the common problems you encounter in multi-threaded environments? How did you solve it?

Common problems encountered in multithreaded and concurrent programs are Memory interfaces, race conditions, deadlocks, live locks, and starvation. The problem is endless, and if you make a mistake, it will be hard to find and debug. This is most interview-based rather than application-based Java threading issues.

Additional questions:

1) what is the difference between a green thread and a local thread in Java?

2) what is the difference between a thread and a process?

3) what is context switching in multi-threading?

4) the difference between deadlock and live lock, 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 an uncatchable exception in a thread?

8) what is a thread group and why is it not recommended in Java?

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

The difference between 10) in Java Executor and Executors?

11) how do I find which thread is using the most CPU time on Windows and Linux?


Related articles: