JAVA interview, at least you should know that!

Teacher Ma said that there are many reasons for leaving employees, but only two points are true:

Money is not in place

The heart was wronged

Of course, I want to exchange a platform, change direction, think about why you want to quit, if you really want to quit, want to get an ideal offer, in addition to luck, have enough solid basic skills, I hope the following interview experience can provide some help for you to.

Project experience

The interviewer in the beginning will let you introduce yourself, is to let you introduce some of the projects you have done, look at your level of understanding of these projects, because a lot of people resume projects are not from A to Z are involved, some just to participate in and implement some modules of it, or take over the maintenance of others, so speak or write in your resume and interview in the process of project experience, you must be able to understand the sequence of events, because the interviewer will according to the description of your project, the realization principle of the project, or why not realize the question, then a. I do not know how to answer, such a situation will only greatly reduce the interview.

Scene dialogue:

Interviewer: (with your resume) tell me about your recent project

Me: &…… % $#*&amp $$also! Say a lot of Chase (don’t know how many interviewers get in, and the interviewer will pick him up and ask questions)

Interviewer: you said netty was used in this project. Can you tell me something about the thread model of netty?

I: (fortunately I read the source code of netty netty) by Reactor model based on the multiplexer receives and processes user requests (you can speak more about it), the internal implementation of two boss thread pool, thread pool and work thread pool, boss thread is responsible for processing the request accept event when receiving the accept event request, encapsulating the socket corresponding to a NioSocketChannel, and work to the work thread pool, thread pool for the requested read and write events (by oral plus drawing, the implementation process request outlined again, time is limited, it is impossible to put all the details all finished, pick point, pick the memories of speaking)

Interviewer: Well, that’s pretty deep. Then, do you have any difficulties or challenges when you’re doing this project?

I: (when the interviewer wants to let you out of their own answers, so be sure to answer, no answer is that you highlight this project, if the problem is not ready, only temporary play, of course I belong to improvise) want a little bit, because before did encounter this problem at the time this project, however, familiar with netty, the business logic request on the work thread for processing, time pressure measurement, found that QPS always do not, after see source code was found, because the business logic is time consuming, completely occupied work thread pool resources, leading to new request has been in a wait state.

Interviewer: How did that end up?

Me: finally, the logic for handling the business is encapsulated as a task and committed to a new business thread pool. After execution, the requested write event is executed by the work thread pool.

Interviewer: OK, do you know that selector in NiO might trigger bug?

Me: Well, yes, selector’s select method, because the underlying epoll function can be idling, resulting in cpu100%.

Interviewer: how do you solve the problem?

I: this issue has been solved by netty, & ^%& $^ (netty solution to say again)

Interviewer: Well, by the way, do you have a program for yourself?

Me: Yes, & &………… $$##@, his project indicators say a, how to carry out AB experiments, how the iterative optimization index

Interviewer: Well, OK, the problem of the project is here first. Let’s take a look at the basic points of java.

A project, just as I do of course, specific analysis of specific projects, not every interviewer asked at all, if the interviewer does not understand netty, questions will naturally pick other problems, but you can also try to put the question to the familiar direction to rely on.

Interview knowledge

1 thread pool

Thread pool implementation principle, this knowledge point is really very important, almost every interview will be asked, the general way of asking questions are as follows:

1, “talk about the thread pool implementation principle”

2. “What’s the difference between coreNum and maxNum in the thread pool?””

3, “how do you set the thread pool parameter in different business scenarios?”

Scene dialogue:

Interviewer: how often do you use the thread pool?

Me: Well, I used it in my project

Interviewer: Well, tell me about the thread pool implementation principle

I: (I have seen before the source, but a long time a bit vague), can you give me a pen and paper, I analyzed to see you, & & $& assuming the initialization of a thread pool, 5 is the core of the number of threads, the maximum number of threads is [email protected]@@

Interviewer: Well, OK, you go on…

Me: draw a square on the paper, which represents a thread pool, when it is initialized, there is no thread inside

Interviewer: Well, OK, you go on…

Me: again, a long thin rectangle, which stands for blocking the queue, has no mission at first

Interviewer: Well, OK, you go on…

I: when a task, in the square to draw a small circle, representing the initialization of a thread, if there is a task, then draw a circle, said then initializes a thread, after continuous draw 5 circles, if sixth tasks…

Interviewer: Well, OK, you go on…

Me: at this point, sixth tasks will be placed in the blocked queue

Interviewer: Well, what about then?

Me: aren’t there 5 threads in the thread pool now? If one of the threads is free, they get sixth tasks from the block queue and perform it

Interviewer: Yes, that’s right. What if the task is faster than the rate of consumption?

Me: if the thread pool’s 5 threads are in the running state, the task is saved first in the blocked queue

Interviewer: what if the queue is full?

I: if the queue is full, we do not set the maximum number of threads is 10, while only 5 threads in the thread pool, then create a thread to execute the task cannot be saved to the blocked queue, and then I drew a circle of 5 square.

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

Me: this situation, through the custom reject function to deal with the task here, and take a mouthful, to think that the end of the question…

Interviewer: Well, then what happens to threads in the thread pool if the task in the blocked queue is done after a period of time?

Me:… This seems to be more than the core thread number of the thread will be automatically recovered within a free period of time… Because a bit do not remember this logic, the answer is a bit empty…

Interviewer: Well, what scenario will happen to this situation?

Me: (sometimes really stupid, a lot of things know, but in the interview, nervous, completely forget) this… That… I do not seem to have encountered such a situation

Interviewer: Well, OK, you can think about it when you get back


I forgot to kill the scene

Thread pool analysis article:

In depth analysis of the java thread pool implementation principle

2, the realization of the lock

During the interview about locks, Synchronized and ReentrantLock are generally asked about the principles of implementation, and even more questions are asked to read and write locks.

Scene dialogue:

Interviewer: what are the locks in Java?

Me: for example, Synchronized and ReentrantLock… There are not many reading and writing locks, so I haven’t studied (I’m afraid I’ve been asked to read and write locks because I haven’t been looking for them)

Interviewer: Well, first of all, let’s talk about the implementation of Synchronized

I: Well, Synchronized is a JVM implementation of the lock, the lock acquisition and release respectively is monitorenter and monitorexit instruction, the lock in the implementation to bias lock, lightweight and heavyweight lock lock, which tend to lock in 1.6 is enabled by default, lightweight locks in the multi thread competition situation will be expanded a heavyweight lock, lock &amp data are stored in the object header; & @@#,… (well, said a lot, the interviewer didn’t interrupt me)

Interviewer: Oh, well, that’s pretty clear. So, what do you think about ReentrantLock?…

Me: ReentrantLock is implemented on AQS

Interviewer: what is AQS?

I will keep a state variable state within the AQS, modify the variable by the value of CAS, successfully modified thread said access to the lock, no modification is successful, or that state state is already locked state, through a Waiter object encapsulates thread, added to the waiting queue, and hanging up wait for wake up & & & $$(a “)

Interviewer: can you tell me what the implementation principle of CAS is?

Me: CAS is implemented through the compareAndSwap method of the unsafe class (a smug smile)

Interviewer: Oh, well, do you know the meaning of the parameter of this method?

I: (this is after me… Tried to think about, because I really have seen ah) I think ah, this time to see a little long, the first parameter is the object to be modified, the second parameter is the offset in the object to modify the variables, the third parameter is modified before the value of the fourth parameter is the expected value. The modified… (say after admire some of their own, remember this, but if the interviewer still refused to let me go…)

Interviewer: Well, yes, do you know how the OS level is implemented?

Me: (I went to your uncle…) I just remember X86 has a CMP at the beginning of the instructions, I forgot the specific…

Interviewer: Well, well, do you know what’s wrong with the CAS directive?

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

Interviewer: what do you mean?

Me: it’s a variable V. If the variable V is read A for the first time, and when it is ready to assign, it is still A. Does that indicate that its value has not been modified by other threads? If the value has been changed to B during that period, and then changed back to A, the CAS operation is mistaken for that it has never been modified.

Interviewer: how do you fix that?

: (I have finished… My heart is breaking) in this case, Java Concurrent package provides a labeled atom reference class “AtomicStampedReference”, it can control the variable values to ensure the correctness of CAS version.

Interviewer: Well, OK, that’s the end of the question. Let’s see something else

Me:… Can I have a drink of water?

Lock analysis on the article, in the hope that useful to everyone:

Easy to understand java synchronizer AQS

CAS in Java

Easy to understand, synchronized

Easy to understand, ReentrantLock

Unsafe in Java

Java volatile keyword puzzle

Depth analysis of Object.wait/notify implementation mechanism

Depth analysis of JVM implementation of synchronized

3, ConcurrentHashMap

When examining the data structure, the interviewer will start asking the realization of the principle of HashMap, when you say HashMap is not thread safe, you may then lead to ConcurrentHashMap, start the following dialogue.

Scene dialogue:

Interviewer: talk about the ConcurrentHashMap implementation principle

Me: No. @ @ [email protected] @ $%% of the sectional lock for $1.8 after the change but based on the way

Interviewer: 1.8 what’s the way?

Me: I talked about the implementation principle of 1.8, in which the red black tree was mentioned…

Interviewer: can you tell me the concept of the red black tree?

Me: red black tree is a two tree, and is balanced…… %…… …… ,

Interviewer: you can talk about the red black tree…..

I: hold on, don’t ask, I only know that he is a red black tree two fork tree, one more than the other tree attributes, I don’t know anything else.

Interviewer: OK, then change it. Do you know how the size method works?

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

Interviewer: that’s right

Me: I remember it seems that the return of the size method is inaccurate, and this method is not used at all times…

Interviewer: if you think the return value of the size method is inaccurate, what do you think should be done if you do it yourself?

I: @ @ @ $… No. two black…

Me: wait a minute. Let me see…… you should be able to record with AtomicInteger variables… Well, yes, every time I insert or delete this variable, I smile…

Interviewer: Oh, yeah? Well, if I think this AtomicInteger is bad, can it be optimized?

I am ignorant: forced face… (at the time was the volitile variable to forget)… No, I can’t think out…

Interviewer: Well, then, you can look at the source code again. It’s already been implemented in jdk…

Me: Oh, yeah?

Interviewer: Well, that’s all for today’s interview. We’ll let you know later.


About ConcurrentHashMap, I also wrote a lot of analysis of the article, I hope useful for all of us:

Easy to understand, ConcurrentHashMap (1.8)

Talk about the different implementations of ConcurrentHashMap1.7 and 1.8

Implementation analysis of red black tree of ConcurrentHashMap

In depth analysis of the expansion of ConcurrentHashMap1.8 implementation

Virtual machine JVM correlation

This is not the content of each interviewer will ask, but if it is for senior position, this link is indispensable, the degree of difficulty of the interview is not the same, some interviewers may have to tell you the memory model of the virtual machine, some will let you explain the garbage recycling, of course, actual combat experience there will be a virtual machine online tuning, troubleshooting etc..

Scene dialogue:

Interviewer: did you know about the Java virtual machine?

I: Well, slightly touched… (water brother said, words can not say too full, easy to face)

Interviewer: Well, first of all, let’s talk about the memory model

I: Java stack, Java stack, program counter, method, permanent generation 1.7, 1.8. Metaspace… (a crackling concept, a brief description of each memory area under use, can think speak out, do not retain, don’t wait for the interviewer asked “what else?”)

Interviewer: Well, how do the Java heaps work?

I: in the HotSpot virtual machine, Java heap into new generation and the old generation, I see is the realization of the 1.7, and all the permanent generation, new generation is divided into Eden and survivor area, survivor area is divided into S0 and S1, or from and to, (this time. I want a pen and paper, because I think this topic can talk quite a long time, and I am more familiar… While drawing, while description) where Eden, from and to, the memory size is 8:1:1 (the default detail to say…), at this time, I have been in the draw new generation and the old generation representative region

Interviewer: Well, tell me about the initialization process of the object in memory

: (I don’t just say, a new Java object in the heap memory allocation and initialization, or memory allocation in Eden and initialization) to initialize an object, first to load the corresponding object class file, the file data is loaded into a permanent generation, and create a bottom the instanceKlass object represents the class, and then to space object allocation initialization, the size distribution of priority thread private memory space, if space is insufficient, and then to the Eden memory allocation of ^& & *%…

Interviewer: Well, OK, let’s talk about the YGC process…

I: to find out the root object, such as object reference object and reference the Java stack static variables refer to the dictionary and system of waiting, the object is marked as active objects, and copied to the to area, then the object reference object in the traversal of these active and mark, find old object reference object relationship and mark in the Eden area, the object of these markers is copied to the to, in the replication process but also to determine the active object GC whether the age threshold has been reached, if the threshold has been reached, is directly promoted to the old age, YGC after the end of the swap and to (reference from can say as much, province the interviewer then asked, I put CMS in the recovery of the old is roughly said again, think the interviewer will skip this topic, or too young).

Interviewer: you just mentioned that when you are in YGC, there may be promotions for some people. What should you do if the promotion fails?

I:… (fragment. For a few seconds, I remember I analyzed this code, but was not impressed) I remember in the marking phase, the head and the corresponding object data object is stored in two stack, if the promotion fails, the head of the object object restoration…

Interviewer: so do you have this kind of situation in your project? What’s the problem?

I:… (which I have not met) to have met once, when analyzing the GC log, after the discovery of YGC, log after GC memory greatened, then check out because the object of promotion of failure. (I vaguely remember an article from a stupid God

Interviewer: (no refutation, continue asking) have you had any experience in VM performance tuning?

I: (to tell the truth, tuning experience is really not much) well, a little bit, not enough, is that when we launched XX, YGC frequent ^^&amp special; ^8&, by adjusting the size of the new generation (virtual machine parameters online environment is the default), and check the business logic code & *& $$~~!

Interviewer: huh? Anything else?

Me: (for an interview so long, so afraid of the interviewer’s next sentence is “en?” Anything else?” Obviously, the interviewer doesn’t answer my question yet, but I can only answer to this point. Well, the experience is really limited. So far, I’ve done some optimizations on this project.



Interviewer: let’s see something else then.

About the virtual machine article, I for the realization of hotSpot wrote some analysis, interested students can see, these articles looked really a bit boring.

Related articles:

JVM source code analysis of the JVM startup process

JVM source code analysis of the heap memory initialization

JVM source code analysis of the Java class loading process

JVM source code analysis of the Java object creation process

JVM source code analysis, how to trigger and execute GC threads

JVM source code analysis of the garbage collection process

JVM source code analysis of the implementation of the new generation of DefNewGeneration

JVM source code analysis of the old TenuredGeneration garbage collection algorithm

Detail questions

Details determine success or failure. In the interview process, although there are luck ingredients exist, but for the details of the master degree, can be a good measure of the examinee’s technical level.


Scene dialogue:

Interviewer: tell me about the implementation of the volatile keyword

Me: the volatile keyword provides memory visibility and no memory reordering

Interviewer: explain them separately

I: because the concept of main memory and working memory in the virtual machine memory, each CPU has its own working memory, when reading a common variable, first reading working memory variable, if there is no corresponding variables in the working memory, from main memory is loaded into the working memory of common variables working memory is modified and not immediately synchronized to the main memory, memory visibility guarantees in multithreaded scenarios, which modify the thread A of the variable, other threads can read the new value of & &%%……

Interviewer: how do you guarantee it?

I: when the volatile of the variable write operation, directly to the new value is written to main memory, the memory for the data variables and empty the other CPU in the working memory, when the volatile of the variable to read, read the main memory data & & &%% [email protected]

Interviewer: do you know how the system level is implemented?

Me: (what, what, are, u says what) I remember the assembler code that operates the volatile variable, with the lock prefix command in front

Interviewer: you’re talking about the code level. I mean system level

I: (not forced…) the bottom down I really didn’t study…

Related articles: “Java volatile keyword puzzle”


Scene dialogue:

Interviewer: tell me about the implementation principle of the finalize method of the Object class

: (I don’t think the interviewer will ask this) a newly created object, in the JVM will determine the object class overrides the finalize method, and finalize method is not empty, the object is encapsulated into a Finalizer object, and added to the Finalizer list.

Interviewer: Well, what about then?

I: the Finalizer class will initialize a FinalizerThread type charge from the thread, to obtain a reference to a Finalizer object in the queue, and execute the runFinalizer method of the Finalizer object, &amp finalize method, finally the implementation of the original object; &%%## (this logic around a bit, then the answer is false)

Interviewer: when will the Finalizer object be in the reference queue?

I: (to think) at the time of their GC, in what specific time point or to be inserted into the reference queue, this realization I have forgotten… (I really forget, just remember that this logic is too complex)

Interviewer: Well, have you verified that the finalize method will be implemented?

I: Well, I have written examples to prove, but also see the source code.

Interviewer: how do you prove it?

I: the initialization of a large array, it is clear that GC is after recovery, then the implementation of System.gc (&amp), the output information in the finalize method; &%%@@ (to verify, had said again)

Interviewer: Well, yes…

The realization of the principle of “in-depth analysis of the Object.finalize method”

big problems

What’s the big problem? That’s the big problem. Let yourself understand and take out all your life lessons

Scene dialogue:

Interviewer: if you are given a system, how do you optimize it?

Me: (optimize what? Performance, stability, or anything else, you can only do it on your own, with a project of your own

1 、 analysis system, define index

2, through the system of submerged, collecting measure index, iterative optimization of &amp index; & ^%& $#

Interviewer: that’s all? No?

I: (because it is the phone interview, feel when the head is blank, and estimate the interviewer’s level is also related to the performance of the interface) if the index is, can see the system memory is not caching can be used to optimize the performance, such as redis, if access is frequent and not often change data. Such as hot data, can directly use the local cache optimization, after all a network request also needs 1~2 MS

Interviewer: is that all right?

I: (because of their system optimization experience did not rich, let the interviewer think that we can only think of optimization so less of it) to read and write the database from the database table depots, if the query database often conditions, can be introduced into es or Lucene to optimize the search service

Good students, I could introduce are introduced to you, the part above is what I want to say, if you want to get high salary in the IT industry, we can participate in the training camp course, choose the most suitable for their own course of study, Daniel Qinshou, 7 months after entering a high salary. Our courses include: Java engineering, high performance and distributed, high performance, easy to understand. Viaduct. Performance tuning, Spring, MyBatis, Netty, source code analysis, and big data. If you want to get paid, want to learn to think, good employment prospects, want to compete with others to gain advantage, to enter the interview but Ali but worried about the interview, you can come, group number: 636532962

Note: adding group requirement

1, with 1-5 working experience, the face of the current popular technology, I do not know where to start, need to break through technical bottlenecks can be added.

2, stay in the company for a long time, easy to live, but job interview rebuffed. Need to study in a short time, job hopping, high salary can be added.

3, if there is no work experience, but the foundation is very solid, the Java working mechanism, commonly used design ideas, commonly used java development framework master skilled, you can add.

4, I feel very cattle B, general demand can be done. But the knowledge is not systematic, it is difficult to continue breakthroughs in the field of technology can be added.

5., Ali Java senior Daniel live broadcast, to explain the knowledge, knowledge sharing, many years of work experience combing and summarizing, with all of you, fully and scientifically establish their own technical system and technical knowledge!

6. trumpet or small white and other groups are not given, thank you.