15 Top Java multithreaded questions and answers

Source: Internet
Author: User
Tags volatile

Multithreading and concurrency issues are an essential part of any Java interview.

1) There are now three threads: T1, T2, and T3. How do you ensure that the T2 is executed after the T1 is complete and T3 executed after the T2 is complete?

This threading issue is usually asked in the first stage or at the phone interview stage to determine whether you are familiar with the "Connect" method. This multithreading problem is relatively straightforward and can be implemented in the Join method.

2) What is the advantage of the Java lock interface than the sync block? You need to implement an efficient cache that allows multiple users to read, but only one user is allowed to write to preserve their integrity. How did you achieve that?

The biggest advantage in multithreading and concurrent programming lock interfaces is that they provide locks for reading and writing separately, which can meet high-performance data structures and conditions that prevent you from writing like Concurrenthashmap. The interview problem with Java threading is increasingly based on the interviewer's answer. I strongly recommend that you read the lock carefully before you make multi-threaded access, because it is now widely used to build client-side caches and transactional connection spaces for electronic transactions.

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

A Java thread interview is often asked in a telephone interview. The biggest difference is that when waiting for a wait, the lock is released and sleep locks the lock. Waits are typically used for inter-thread interaction, while hibernation is typically used to pause execution.

4) The blocking queue is implemented in Java.

This is a more difficult multi-threaded interview problem, it can achieve a lot of goals. First, it detects whether a test taker can write a program with a Java thread. Secondly, it can detect the candidates ' understanding of the concurrency scene, and put forward many problems on this basis. If he uses the wait () and notification () method to implement the blocking queue, you can ask him to write the latest Java 5 concurrency.

5) write the code in Java to solve the problem of producer consumers.

This is similar to the question above, but it is a classic question, and sometimes the interview asks the following questions. There are, of course, a lot of solutions on how to solve Java producer consumer problems, and I've shared a way to block queues. Sometimes they even ask how to make a philosopher's meal.

6) Programming program, using Java may lead to deadlock, how will you solve it?

This is my favorite Java thread interview problem, because even a very common deadlock problem when writing multithreaded concurrent programs, many candidates cannot write deadlock-free code (no deadlock code). They are struggling. Just tell them you have n resources and n threads, and you need all the resources to do an operation. To simply replace N with 2, the more general the data is, the more complex the problem looks. More information about deadlocks is done by avoiding the deadlock in Java.

7) What is atomic operation and what is the atomic operation of Java?

A very simple Java threading interview problem, the next problem is that you need to synchronize an atomic operation.

8) What is the key role of volatility in Java? How do you use it? What is the difference in Java from the synchronization method?

Threading issues based on keyword volatile are getting more and more attention due to changes in Java 5 and Java memory patterns. You should be ready to answer how you can ensure the visibility of volatile variables, sequentility, consistency in the parallel environment.

9) What are the conditions for competition? How do you find and solve the competition?

This is a problem that occurs at the advanced stage of a multithreaded interview. Most interviewers are asking you about the competitive environment you have recently encountered and how you can solve these problems. Sometimes they write simple code and then let you find the code's competitive conditions. You can refer to my previous article for the Java race condition. In my opinion, this is one of the best Java thread interview questions. It can detect the candidate's experience to solve competition conditions exactly the same, or write, which is free of data race or race code, "This is the best book" Java "parallel practice.

10) How do I use thread dumps? How will you analyze the thread dumps?

In Unix, you can use Kill-3, the thread dump will print the log, and you can use "Ctrl + Break" in Windows. This is a very simple and professional thread interview problem, but if he asks you how to analyze it, it will 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 a very classic Java multithreading interview problem. This is also a problem when I first started to write a thread program. Now the problem is usually in the phone interview or in the first intermediate Java interview first round of questions. 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. However, if you call the run () method directly, it does not create a new thread and does not execute code that invokes the thread. Read the article I've written earlier about the differences between startup and run methods to get 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 a lot of solutions. If the thread is blocked by IO, I think there is no way to stop the thread. If the thread waits for blocking by calling (), sleep (), or join (), you can break the thread and throw it to wake up the interruptedexception. How to handle blocking methods in Java, as I wrote earlier, there is a lot of information about handling blocking threads.

13) What is the difference between Java Cyclibarriar and Countdownlatch?

The problem with this thread is primarily to detect if you are familiar with JDK5 and contract. The difference is that cyclicbarrier can reuse the barriers that have passed and Countdownlatch cannot be reused.

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

Another multithreaded classic interview problem is thread-independent, but it helps a lot. This Java interview problem is tricky if he asks you to write an immutable object, or ask you why the string is immutable.

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

Often encountered multi-threaded and memory interfaces, craft complex race conditions, deadlocks, live locks and starvation. There is no end to this problem. If you make a mistake, it's hard to find and debug. This is the most basic interview, not a Java threading problem, according to the actual application.

Follow us on the "Java trade union" to get more technical dry Goods:

15 Top Java multithreaded questions and answers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.