15 Advanced Java multi-threaded face questions and answers _java

Source: Internet
Author: User
Tags data structures volatile

Java Thread Interview Question

Multithreading and concurrency issues are an essential part of any Java interview. If you want to get a front desk post for any equity investment bank, then you should prepare a lot of questions about multithreading. Multithreading and concurrency are a very popular topic in investment banking, especially in the development of electronic transactions. They will ask the interviewer a lot of confusing Java threading problems. The interviewer just wants to make sure that the interviewer has enough Java threading and concurrency knowledge, because a lot of the candidates float on the surface. High-capacity and low-latency electronic trading systems for direct market transactions are inherently concurrent. Here are some Java threading issues I like to ask at different times and places. I did not provide the answer, but as long as possible I will give you clues, sometimes these clues enough to answer the question. Now the problem of referencing Java5 and contracting the concurrency tools and concurrent collections is increasing. Among those issues threadlocal, Blocking Queue, counting semaphore and concurrenthashmap are more prevalent.


15 Java Multi-threaded face questions and answers

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

This threading problem is usually asked in the first round or during the phone interview to see if you are familiar with the join method. This multithreading problem is relatively simple and can be implemented by the Join method.

2 What is the advantage of the lock interface in Java than the synchronized block? You need to implement an efficient cache, which allows multiple users to read, but only allow a user to write, in order to maintain its integrity, how would you implement it?

The biggest advantage of the lock interface in multithreaded and concurrent programming is that they provide a lock for both read and write, which can satisfy your high performance data structures like CONCURRENTHASHMAP and conditional blocking. The question of the Java thread interview is increasingly based on the interviewer's answer. I strongly recommend that you read the locks carefully before you go to a multithreaded interview, as it is currently heavily used to build the client cache and transaction connectivity space for the electronic trading final.

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

Java thread interview questions that are often asked during a phone interview. The biggest difference is that wait will release the lock while sleep holds the lock. Wait is usually used for interaction between threads, and sleep is usually used to suspend execution.

4 block the queue with Java implementation.

This is a relatively difficult multi-threaded interview problem, it can achieve a lot of purposes. First, it can detect whether a candidate can actually write a program with Java threads, and second, you can test the candidate's understanding of concurrent scenarios, and you can ask a lot of questions based on that. If he uses the Wait () and notify () method 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 the producer-consumer problem.

Similar to the above question, but the question is more classic, sometimes the interview will ask the following questions. There are many solutions to how to solve producer-consumer problems in Java, and I've shared a way to implement blocking queues. Sometimes they even ask questions about how to achieve a philosopher's meal.

6 with Java programming A will lead to deadlock program, how will you solve?

This is my favorite Java thread interview problem, because even if the deadlock problem is common when writing multithreaded concurrent programs, many candidates cannot write deadlock free code (no deadlock codes). ), they are struggling. Just tell them that you have n resources and n threads, and that you need all the resources to do an operation. For simplicity, where n can be replaced with 2, 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 operation, what is the atomic operation in Java?

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

8 What is the key role of volatile in Java? How to use it? How does it differ from the synchronized method in Java?

Since the Java 5 and Java memory models have changed, threading issues based on the volatile keyword have become more prevalent. You should be prepared to answer questions about how volatile variables ensure visibility, order, and consistency in a concurrency environment.

9 What is the competitive condition? How do you find and solve competition?

This is a problem that appears at the advanced stage of a multithreaded interview. Most interviewers will ask about the competitive conditions you've met recently, and how you've solved it. There are times when they will write simple code and then let you detect the competitive conditions of the code. Refer to my previous post on the Java competition criteria. It seems to me that this is one of the best Java thread interview questions that can detect the candidate's experience in resolving competitive conditions, or writing code which is free of data race or any other race condition (this which Are free of data race will not be translated. The best book on this is "concurrency practices in Java".

10 How will you use thread dump? How will you analyze thread dump?

You can use Kill-3 in Unix, and thread dump will print logs, and you can use "Ctrl+break" in Windows. Very simple and professional thread interview questions, but if he asks how you analyze it, it will be tricky.

11 Why we call the start () method executes the run () method, why can't we call the Run () method directly?

This is another very classic Java multi-threaded interview problem. This is also the confusion that I just started to write thread routines. Now this question is usually asked in the first round of a phone interview or in a junior middle-class Java interview. 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 or execute the calling thread's code. Read the article "difference between start and run methods" I wrote earlier to get more information.

How do you wake up a blocked thread in Java?

This is a tricky question about threading and blocking, and it has many solutions. If the thread encounters IO blocking, I do not think there is a way to abort the thread. If the thread is blocked by invoking the wait (), the Sleep (), or the Join () method, you can get the interrupt thread and wake it up by throwing interruptedexception. I previously wrote "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 whether you are familiar with concurrent packages in JDK5. The difference between the two is that cyclicbarrier can reuse the obstacles that have been passed, while Countdownlatch cannot be reused.

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

Another multi-threaded classic interview problem, not directly related to the thread, but indirectly help a lot. This Java interview problem can be tricky if he asks you to write an immutable object, or ask you why a string is immutable.

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

Multithreading and concurrent programs often encounter memory-interface, competitive conditions, deadlocks, live locks, and starvation. The problem is endless, and if you make a mistake, it will be difficult to find and debug. This is most based on the interview, not the Java threading problem based on the actual application.

Several other questions to add:

1 What is the difference between green and native threads in Java?

2 What is the difference between a thread and a process?

3 What is context switching in multiple threads?

4 The difference between a deadlock and a live lock, the difference between a dead lock and a pie?

5 What is the thread scheduling algorithm used in Java?

6 What is thread scheduling in Java?

7 How do you deal with an unhandled exception in a thread?

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

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

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

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

Related Article

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.