Please note the original source, thank you!

From the Emperor to the common people, all take self-cultivation as the basis.

As Teacher Ma once said, there are many reasons for employees’ dimission, and only two are the most true:

  • Money, not enough
  • Heart, wronged

Of course, I want to change my platform and direction and figure out why I want to change my job. If I really want to change my job and get an ideal offer, I need to have good luck and solid basic skills. I hope the following interview experience can provide some help to you.

# # # project experience The interviewer will help you to introduce myself in the first place, main is to want to let you introduce myself to you have done some projects, take a look at your understanding of these projects, because many projects are not written in the resume from beginning to end, some just to participate in and implemented some of the module, or a maintenance project, So write in your resume and interview project experience, you must be able to understand, because the interviewer will certainly according to your project description, to the implementation principle of the project, or a question, why implement not wooden live and do not know how to answer at this moment, so the situation will only reduce the interview.

Interviewer: (holding your resume) Tell me about your latest project

I: &… % % % % % % % % % % “(I don’t know how much the interviewer will listen, the interviewer will pick the questions he knows)

Interviewer: You said that netty was used in this project. Can you give a general idea of netty’s threading model?

I: Netty uses the Reactor model to receive and process user requests based on a multiplexer (as much as I can tell). Netty internally implements two thread pools, the boss thread pool and the Work thread pool. The boss thread pool is responsible for processing the accept event. When a request for an accept event is received, the corresponding socket is encapsulated in a NioSocketChannel and handed to the work thread pool, which is responsible for the read and write events of the request. It is also impossible to cover all the details. Choose the key points and the memorable ones.)

Interviewer: Well, that’s a pretty deep understanding… Did you have any difficulties or challenges with this project?

I: (when the interviewer want to let you write their own answers, so be sure to answer, can highlight out you this project didn’t answer, if there is no preparation, can only be played a temporarily, of course I am belong to the temporary play) think about it a little, because before really encountered this problem, when doing this project at the time, but of netty familiar, We put the business logic of the request into the thread of the work thread pool for processing, and found that the QPS could not get up all the time when we pressed it. Later, after reading the source code, we found that the processing of the business logic was time-consuming, which completely occupied the resources of the work thread pool, resulting in the new request has been in the waiting state.

Interviewer: How was it settled in the end?

I: Finally, encapsulate the business logic into a task and submit it to a new business thread pool for execution. After execution, the Work thread pool will execute the requested WRITE event.

Interviewer: Ok, did you know that selector in NIO can trigger a bug?

Me: Well, that’s right, the selector select method, because the underlying epoll function could be idling, causing cpu100 percent.

Interviewer: How do you solve the problem?

Me: This problem has been solved in Netty by &^%&$^ (say netty’s solution again)

Interviewer: Oh, by the way, do you have any targets for this project?

Me: Yes, &&………… How to conduct AB experiment, how to iterate and optimize the index

Interviewer: Well, that’s all for the project. Let’s take a look at the basics of Java.

The above is just a project I have done, of course, it depends on the project, not every interviewer will ask the same point, if the interviewer does not know netty, naturally choose another question to ask, but you can also try to ask the question in the direction of your familiar.

### Interview knowledge points

####1, thread pool thread pool implementation principle, this knowledge is really important, almost every interview will be asked, general questions are as follows: What is the difference between coreNum and maxNum in thread pool? 3. How to set thread pool parameters in different business scenarios?

Interviewer: How often do you use the thread pool?

Me: Well, I used it in my *** project

Interviewer: Ok, tell me how thread pools work

I :(I have read the source code before, but the time is a little fuzzy), can you give me a pen and paper, I draw a picture analysis for you to see, &&¥& suppose initialization of a thread pool, the number of core threads is 5, the maximum number of threads is 10@@@ @

Interviewer: Well, ok, you go on…

Me: I drew a square on the paper, this represents a thread pool, when initialized, there are no threads in it

Interviewer: Well, ok, you go on…

Me: I drew a long rectangle again, this one represents the blocking queue, and there were no tasks in it at first

Interviewer: Well, ok, you go on…

Me: When a task comes, I draw a small circle in the square, which means I initialized a thread. If another task comes, I draw another circle, which means I initialized another thread. After 5 circles, if the sixth task comes…

Interviewer: Well, ok, you go on…

Me: the sixth task will be put on the blocking queue..

Interviewer: Well, then what?

Me: There are now 5 threads in the thread pool, and if one thread is idle, it will fetch the 6th task from the blocking queue and execute it..

Interviewer: HMM, right. What if tasks are generated faster than they are consumed?

Me: If all five threads in the thread pool are in the running state, then the task is stored in the blocking queue first

Interviewer: What if the line is full?

Me: If the queue is full, didn’t we set the maximum number of threads to be 10, and there are only 5 threads in the thread pool, a new thread will be created to execute the task that can’t be saved to the blocking queue, and THEN I draw 5 circles in the square.

Interviewer: What if the number of threads in the thread pool reaches 10 and the blocking queue is full?

Me: This is a custom reject function, so I’m done with the task.

Interviewer: Ok, what happens to the threads in the thread pool if, after running for a while, the tasks in the blocking queue run out?

I:… Threads that seem to exceed the number of core threads are automatically reclaimed for a period of idle time. Because I don’t remember this logic, the answer is a little empty…

Interviewer: Ok, under what circumstances does this happen?

I :(sometimes really stupid ah, a lot of things know, but in the interview when a nervous, all forget) this… The… I don’t think I’ve ever come across a situation like this

Interviewer: Well, ok, think about it when you get back

I:…

I can’t believe I forgot the seckill scene

Thread pool analysis article: in-depth analysis of Java thread pool implementation principles

####2, The implementation of lock In the interview process, generally asked about the implementation of Synchronized and ReentrantLock, more often ask about read and write lock.

Interviewer: What locks do you know about in Java?

Me: Things like Synchronized and ReentrantLock… Read/write lock is not used much, so there is no research (I am afraid of being asked about read/write lock, because I have not looked)

Interviewer: Well, let’s start with the principle of Synchronized

I: Well, Synchronized is a lock implemented by JVM, whose acquisition and release are monitorenter and Monitorexit directives, respectively. Synchronized is implemented as biased lock, lightweight lock, and heavyweight lock. Biased lock is enabled by default in 1.6. Lightweight locks can swell to heavyweight locks in multi-threaded contention, and lock data is stored in the object header… &&@@#, (Yeah, a lot, and the interviewer didn’t interrupt me)

Interviewer: Oh, well, that’s pretty clear. Tell me about the implementation of ReentrantLock…

Me: ReentrantLock is implemented based on AQS

Interviewer: What is AQS?

I: A state variable state will be stored inside the AQS, and the value of this variable will be changed via CAS. The thread that has successfully modified will acquire the lock. If the thread has not successfully modified, or if it finds that the state state is locked, it will use a Waiter object to encapsulate the thread, add it to the waiting queue, suspend it and wait to be waked up. (More)

Interviewer: Can you talk about how CAS is implemented?

Me: CAS is implemented via the Unsafe class’s compareAndSwap method.

Interviewer: Oh, ok, do you know what the parameters of this method mean?

Me :(this is forcing me… The first parameter is the object to be modified, the second parameter is the offset of the variable in the object to be modified, the third parameter is the previous value to be modified, and the fourth parameter is the expected value…. (AFTER I said it, I was a little impressed by myself. I remember this, but the interviewer still seemed to refuse to let me go…)

Interviewer: Um, right, do you know how the operating system level is implemented?

Me :(I go to you big ye…) I only remember a CMP starting instruction in X86, which I forgot…

Interviewer: Well, ok, do you know what the disadvantages of CAS instructions are

Me: Oh, the disadvantage of CAS is the ABA problem

Interviewer: What do you mean?

Me: it is A variable V. If variable V is A when it is first read, and it is still A when it is ready to assign, does that mean that its value has not been modified by other threads? If its value had been changed to B and then back to A during that time, the CAS operation would have assumed that it had never been changed.

Interviewer: How do you solve that?

Me :(endless ah… For this scenario, Java packages a labeled atom reference class “AtomicStampedReference” that guarantees the correctness of CAS by controlling the version of the variable value.

Interviewer: Well, ok, that’s all for now, and let’s move on to something else

I:… If I could get a drink of water

Article on lock analysis, I hope to be useful to you: Java AQS CAS ReentrantLock Java volatile keyword unexplored Object. Wait /notify implementation mechanism Dive into the JVM implementation of Synchronized

####3, ConcurrentHashMap When looking at data structures, the interviewer will start by asking how HashMap works. Once you say that HashMap is not thread-safe, you will ask yourself to elicit ConcurrentHashMap, which may lead to the following conversation.

Interviewer: Talk about how ConcurrentHashMap works

Based on the segmentation lock % % ¥# @ # ¥, but after 1.8 changed the implementation

Interviewer: 1.8 what way

Me: I talked about how 1.8 works, including red black trees…

Interviewer: Can you explain the concept of red-black trees

Me: A red-black tree is a binary tree, and it’s balanced… %…… Selections… .

Interviewer: Can you talk about red black trees…

Me: Stop asking, red black tree, I only know it is a binary tree, one more property than the others, I don’t know the others. Interviewer: Ok, change, do you know how its size method is implemented?

Me: Size method? Do you want to get the number of elements in the Map?

Interviewer: That’s right….

Me: I remember that the size method return is not accurate, usually do not use this method…

Interviewer: If you think the size method returns an inaccurate value, how would you implement it yourself?

I:… @ # selections @ @… A black eyes

Me: Wait, let me think….. It should be possible to record with the AtomicInteger variable… Yeah, I smirk every time I insert or delete this variable…

Interviewer: Oh, yeah, and if I don’t think the AtomicInteger variable is performing well, can I optimize it?

Me: Meng force face… (I forgot the volitile variable)… I don’t think so. I can’t think of anything…

Interviewer: Well, go back and look at the source code. It’s implemented in the JDK…

Me: Oh, yeah….

Interviewer: That’s all for today. We’ll let you know later.

I:…