15 Top Java multithreaded questions and answers

Source: Internet
Author: User

1) Now there are T1, T2, T3 three threads, how do you ensure that the T2 after the T1 execution, T3 after the implementation of T2 execution?

This thread problem is usually asked during the first or phone interview phase to detect whether you are familiar 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 in Java than the synchronized block? You need to implement an efficient cache that allows multiple users to read, but only one user is allowed to write in order to maintain its integrity, how will you implement it?

The biggest advantage of the lock interface in multi-threaded and concurrent programming is that they provide a lock for both read and write, which satisfies high-performance data structures and conditional blocking that you write like Concurrenthashmap. The question of a Java thread interview is increasingly based on the interviewer's answer. 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 transaction connection space for the electronic trading final.

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

Frequently asked questions about a Java thread interview in a phone interview. The biggest difference is that wait waits for the lock to be released while sleep holds the lock. Wait is typically used for inter-thread interaction, and sleep is typically used to pause execution.

4) block queues with Java implementation.

This is a relatively difficult multi-threaded interview problem, it can achieve a lot of purposes. First, it can detect whether the candidate can actually write programs with Java threads, and second, can detect the candidate's understanding of the concurrency scenario, and you can ask many questions according to this. If he uses the Wait () and notify () methods to implement the blocking queue, you can ask him to write again with the concurrency class in the latest Java 5.

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

Similar to the question above, but the problem is more classic, sometimes the interview will ask the following questions. How to solve the producer-consumer problem in Java, of course there are a lot of solutions, I have shared a blocking queue implementation method. Sometimes they even ask how to make the Philosopher's meal a problem.

6) How will you solve a program that will lead to deadlocks in Java?

This is my favorite Java thread interview problem, because even deadlock problems are common when writing multithreaded concurrent programs, but many candidates are not able to write deadlock free code (no deadlock codes?). ), they struggled. Just tell them that you have n resources and n threads, and you need all the resources to complete an operation. For the sake of simplicity here the n can be replaced with 2, and the larger the data will make the problem look more complicated. Get more information about deadlocks by avoiding deadlocks in Java.

7) What is atomic manipulation and what is atomic manipulation in Java?

A very simple Java thread interview problem, 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 does it differ from the synchronized method in Java?

Thread issues based on volatile keywords have become more prevalent since the Java 5 and Java memory models have changed. Be prepared to answer questions about how volatile variables ensure visibility, sequencing, and consistency in a concurrency environment.

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

This is an issue that appears in the advanced stages of a multithreaded interview. Most interviewers will ask about the competitive conditions you've met recently and how you've solved them. Some time they will write simple code, and then let you detect the code's competitive conditions. You can refer to the article I published earlier about the Java competition criteria. In my opinion this is one of the best Java thread interview questions that can be used to accurately detect candidates ' experience in solving competitive conditions, 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 would you analyze thread dump?

In Unix you can use kill-3, then thread dump will print the log and you can use "Ctrl+break" in Windows. Very simple and professional thread interview questions, but if he asks you how to analyze it, it will be tricky.

11) Why do we not call the run () method directly when we call the start () method?

This is another very classic Java multithreading interview problem. It was also a puzzle when I first started writing a thread. Now this question is usually asked in the first round of a phone interview or in the middle of a junior 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 does not create a new thread and does not execute code that invokes the thread. Read the "Start and run method differences" Article I wrote earlier to get more information.

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 workarounds. If the thread encounters IO blocking, I do not think there is a way to abort the thread. If the thread is blocking because of a call to wait (), sleep (), or the Join () method, you can break thread and wake it up by throwing interruptedexception. I've written earlier 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 threading problem is primarily used to detect if you are familiar with the concurrent packages in JDK5. The difference between the two is that cyclicbarrier can reuse the barriers that have been passed, while Countdownlatch cannot be reused.

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

Another multithreaded classic interview problem is not directly related to threads, but indirectly helps a lot. This Java interview problem can be tricky, if he asks you to write an immutable object, or asks you why the string is immutable.

15) What are the common problems you encounter in a multithreaded environment? How did you deal with it?

Memory-interface, race conditions, deadlocks, live locks, and starvation are often encountered in multithreaded and concurrent programs. The problem is endless, and if you get it wrong, it will be hard to find and debug. This is most of the interview-based, not the actual application-based Java threading problem.

A few additional questions to add:

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

2) What is the difference between threads and processes?

3) What is context switching in multi-threading?

4) What is the difference between a deadlock and a live lock, the difference between a deadlock and a pie?

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

6) What is thread scheduling in Java?

7) How do you handle non-catching exceptions in the thread?

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

9) Why is it better to use the executor framework than to create and manage threads with apps?

10) What is the difference between executor and executors in Java?

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

--Go to "Code farming Network"

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.