preface
If you look at the Java interview questions over the years, they’re pretty much the same. If you take a closer look, the HashMap is too common. Even the knowledge points are the same, what hash collision ah, concurrent problems ah! For example, THE JVM, no matter the memory structure, GC algorithm and so on! Therefore, if it is for the interview, there are ways to prepare! Remember, no matter how good the foundation is, the interviewer will not be able to ask unconstrained questions, so it is necessary to brush the interview questions!
Analysis of interview questions
What is the underlying structure of Map? (HashMap)
Remark: old topic, everybody interview personnel must memorize!
Answer: A Map is a key-value pair that stores objects. The underlying Map is actually an array and a linked list.
When using the put method, we first find out if there is an object in the array position, and then use key.hashcode to mod the array length. If there is an object in the linked list whose key value is the same as that of the passed key, if there is an object in the linked list whose key value is the same as that of the passed key, if there is an object in the linked list whose key value is the same as that of the passed key, if there is an object in the linked list whose key value is the same as that of the passed key, if there is no object, add() to the linked list directly.
When using the get method, we first find out whether there is an object in the array position, and then use key.hashcode to mod the array length. If it does not exist, it will return null; if it does, it will traverse the linked list to determine whether there is any object in the linked list whose key value is the same as the key value passed; if there is, the value corresponding to the key value will be taken out and returned; if there is no, it will return null;
Thread-safe Map (concurrentHashMap) briefly explains the differences between 1.7 and 1.8. I want to ask if I want to go into more detail (source level), but the interviewer says no.
Note: The same topic, if you have time, again to understand the various methods to solve the HashMap thread safety, and the principle! This question is only a general answer to the structure change, because the implementation code has been changed, the details can be said for a long time, I guess the interviewer does not have time to listen!
Answer:
Jdk1.7 uses Segment + HashEntry to implement the structure as follows:
The meaning of the Segment array is to divide a large table into multiple small tables for locking, and each Segment element stores a HashEntry array + linked list, which is the same as the data storage structure of HashMap
While the jdk1.8
Remove Segment + HashEntry + Unsafe implementation,
Synchronized + CAS + Node + Unsafe
Its structure diagram is as follows:
As shown above, the Segment field is removed and nodes are stored in the array. It is similar to the HashEntry definition in a HashMap, but with some differences. It sets volatile synchronization locks on the value and next attributes, and does not allow calls to setValue to change the Node value field directly.
In addition, the original data structure of table array + one-way linked list is changed to the structure of table array + one-way linked list + red-black tree. In case of too many hash collisions, the linked list will be transformed into red-black tree.
3. How large is the amount of data and concurrency of MySQL project?
Comments: the direction of this question will change depending on your answer.
About capacity: single table row number more than 5 million rows or single table capacity more than 2GB, this time to answer the sub-database sub-table middleware! The topic behind the direction of myCAT, sharing-JDBC and other sub-database sub-table middleware underlying principle!
Regarding concurrency: if the number of concurrent requests exceeds 1200, use middleware such as MQ or Redis as a compensation measure rather than directly working with the database. The topic behind that is redis, MQ principle!
Given that the interviewer is still a fresh graduate, I would hazard a guess
Answer: the amount of data is estimated to be three or four million, and the amount of concurrency is about five or six hundred!
How much do you know about databases?
Note: Since the amount of data and concurrency you are answering is small, there is no question about middleware. Therefore, topics go to the bottom of the database! Plus, with this lead question, the interviewer is giving you a chance to focus on what you do best!
Answer: Learn about common database tuning methods, index optimization, and more!
5, you talk about database index implementation and non-primary key secondary index
Remark: This question is based on your answer above! Remember to lead to their best database basic knowledge! The default is to answer the mysql database
Answer:
From the perspective of data structure:
B-Tree index, data structure is a B+ Tree.
Hash index. The Hash index compares the Hash value after the Hash operation. Therefore, it can only be used for equivalent filtering, not for range-based filtering. Hardly!
R-tree index, only support the geometry data type, also basic do not use!
For secondary indexes that are not primary keys, this actually asks for non-clustered indexes! A non-clustered index is itself a B+ tree whose root node points to the B+ tree of the clustered index.
6. The project uses SpringBoot. Can you tell the difference between SpringBoot and Spring?
Remarks: Basic questions, can spring Boot, basic answer up. Even if not prepared, on the spot thinking can be answered! Also belongs to the guide!
Answer:
-
Spring Boot allows you to build standalone Spring applications;
-
Containers like Tomcat, Jetty, and Undertow are embedded, which means you can run them without having to deploy them.
-
No more tedious configuration of XML files like Spring;
-
Spring can be configured automatically;
-
It provides some existing functions, such as metrics tools, form data validation, and some third-party functions such as external configuration.
-
The POM is provided to simplify Maven configuration
7. How does SpringBoot automatically configure?
Remark: This question is also based on your seventh question, a further question.
Answer:
Why is automatic configuration required?
As the name suggests, the point of auto-configuration is to take advantage of this schema instead of configuring the TEDIOUS XML schema. Before using Spring MVC, you need to configure component scanning, scheduler, view parser, etc. After using Spring Boot automatic configuration, you only need to add MVC components to automatically configure the required beans. All auto-configuration implementations are in spring-boot-Autoconfigure dependencies, including auto-configuration for Spring MVC, Data, and other frameworks.
How does the spring-boot-autoconfigure dependency work?
Spring-boot-autoconfigure dependencies work simply by being initialized with the @enableAutoConfiguration core annotation and scanning the ClassPath directory for auto-configured class dependencies. For example, does the project add the Starter dependency of Thymeleaf? If so, the required beans are automatically initialized by following certain rules to get the default configuration.
You can go on and explain how the @enableAutoConfiguration annotation works! But it is too long, answer up to that point is enough!
8, MyBatis defined interface, how to find the implementation?
Note: The underlying principle of Mybatis, investigate whether you have seen the principle of Mybatis. The blogger has just written mybatis, so this question happens to answer.
Blogger’s inner thought :” Now the school recruitment is so awesome!”
Answer: There are five steps
-
1. Mapper interface is registered at the initial SqlSessionFactory.
-
2. The Mapper interface is registered in the HashMap named MapperRegistry class, key = Mapper class value = create the factory of the current Mapper.
-
3. After the Mapper is registered, you can get the Mapper from SqlSession
-
Sqlsession. getMapper uses JDK dynamic proxy to generate the proxy object of the target Mapper interface.
-
5. The proxy class of dynamic proxy is MapperProxy, where the adding, deleting, changing and checking methods are finally called.
Java memory structure
Comments: basic topic, this should learn JAVA will! Send questions! JVM memory structure
Answer: The JVM memory structure has three main blocks: heap memory, method area, and stack. Heap memory is the largest chunk of the JVM, consisting of the young generation and the old generation. The young generation memory is divided into three parts: Eden space, From Survivor space, and To Survivor space. By default, the young generation is allocated in a 8:1:1 ratio.
The method area stores class information, constants, static variables, and other data. It is shared by threads. To distinguish it from the Java Heap, the method area is also nicknamed non-heap. The stack is divided into Java virtual machine stack and local method stack mainly used for method execution.
Is the object GC?
Note: This question is asking how the JVM can tell if an object needs to be reclaimed! Don’t answer reference counting, just answer reachability analysis algorithm.
Answer:
The basic idea of this algorithm is to start with a series of objects called “GC Roots” and search down from these points. The search path is called the reference chain. When an object is not connected to GC Roots by any reference chain, it is proved that the object needs to be recycled.
As shown in figure:
In the figure above, objects O3 and O4 are not reachable by any GC Roots. All these two objects are not available and need to be collected by GC
Java objects available as GC Roots include the following:
-
Object referenced in the virtual machine stack
-
The object referenced by the class static property in the method area
-
The object referenced by the output in the method area
-
Objects referenced by JNI in the local method stack
Minor GC and Full GC
Comments: Basic questions, will JVM tuning, basic will! I’m just surprised I didn’t ask the Major GC. Let’s also answer the Major GC.
Answer:
Heap memory is the largest chunk of the JVM made up of young and old generations.
Then, collecting memory from the young generation space (including Eden and Survivor regions) is called the Minor GC.
The Major GC is to clean up the old era.
Full GC cleans the entire heap space – both young and old generations.
Garbage collection algorithm
What are the algorithms for garbage collection? The interviewer won’t have the patience to listen to you memorizing algorithm concepts one by one!
Answer:
Mark – clearing algorithm, mark collation algorithm, copy algorithm, generation collection algorithm
Garbage collector G1
Comments: The above questions ask more deeply. The JVM can be configured with different collectors. Examples are Serial, Parallel, and CMS. Take Serial Collector, which in the younger generation was a Collector that used a mark-copy algorithm. In the old days, the mark-sweep-tidy algorithm was used.
In addition, there are many points you can ask about the G1 collector. The author of the question does not describe exactly what point the G1 collector is asking, so let’s answer the concept.
If I were asking, I’d just give you the scenario and ask you which recycler to use. Just ask the recycler, and that’s easier!
Common parameters:
-xx :+UseSerialGC: Use serial collector in new generation and old generation
-xx :+UseParNewGC: Use the parallel collector in the new generation
// There are too many of them.
Answer:
The G1 GC is one of the new Jdk7 features, and the Jdk7+ version can be configured autonomously with G1 as a JVM GC option. G1 divides the heap into equal chunks (each called a region). Each chunk has contiguous memory. Each chunk also acts as Eden, Survivor, and Old, but they are not fixed, making memory usage more flexible. As shown in the figure below
When a garbage collection is performed, the collection thread runs concurrently with the application thread during the tagging phase. After the tagging is complete, G1 knows which blocks are basically garbage, with few viable objects, and starts with these blocks because they free up a lot of free space quickly. That’s why G1 was named garbage-first.
14. Have you used ElasticSearch and Hbase in your project? Do you know their tuning skills well?
ElasticSearch and Hbase are only demo versions of ElasticSearch and Hbase. You don’t usually get into tuning techniques! If this problem answers in-depth understanding, is to dig a hole for oneself! Because the answer to this question is so broad!
Answer: not in depth!
Implementation of Spring RestTemplate
Comment: the blogger who asked this question is confused! If I were to ask, I would start by asking why Spring RestTemplate is chosen when there are so many clients accessing Rest services. And then there’s the principle. I am a little confused by the sudden appearance of a concrete implementation.
Answer:
RestTemplate is a facade framework like SL4fj, which essentially adds a mask to an Http network request and has no implementation of its own.
The ClientHttpRequestFactory interface class supports HTTP access from multiple httpClients.
The RestTemplate then encapsulates the low-level details of assembling, sending HTTP messages, and parsing responses.
That’s fine. Do you want to draw a reference diagram of the relationship between the classes? That’s not realistic!
Describe a web page Http request to the back end of the entire request process
Comment: basic topic, feeling belongs to common sense topic! Will be!
Answer:
Use DNS for domain name resolution –> initiate TCP 3-way handshake –> initiate HTTP request after establishing TCP connection –> Server responds HTTP request, browser gets HTML code –> browser parses HTML code, And request resources in HTML code (such as JS, CSS, images, etc.) -> browser to render the page to the user
Common methods of multithreading and interface classes and thread pool mechanism
Comment: basic topic, basic meeting point thread knowledge, more or less can answer point! But this problem, I feel the scope is a little big ah! May be the author did not express clearly!
Answer:
Common methods:
start,run,sleep,wait,notify,notifyAll,join,isAlive,currentThread,interrupt
Common interface classes:
Runnable, Callable, Future, FutureTask
Thread pool mechanism:
In object-oriented programming, creating and destroying objects is time consuming because creating an object requires memory resources or more. So one way to improve the efficiency of a server program is to minimize the number of objects created and destroyed, hence the pooling technique! .
A simple thread pool consists of the following four components:
-
ThreadPoolManager: used to create and manage thread pools
-
Workthreads: Threads in a thread pool
-
Task interface: The interface that each Task must implement in order for a worker thread to schedule the execution of the Task
-
Task queue: Used to store unprocessed tasks. Provide a buffer mechanism
Summary of my Java foundation is still good, but some mainstream framework source code is still in the state of use, need to continue to see the source code
Note: Frankly, I don’t see any issues that show that the mainstream frameworks are still in use.
19, deadlocks
Answer:
Deadlock refers to two or more than two processes in the process of execution, due to competition for resources of the phenomenon of a kind of wait for each other, without external force, they will not be able to push down, if sufficient system resources, process resource requests can be met, the possibility of deadlocks occur is very low, otherwise it will be because of competing for the limited resources into a deadlock.
The main causes of deadlocks are:
-
(1) Because of insufficient system resources.
-
(2) The order of process operation and promotion is not appropriate.
-
(3) Improper allocation of resources.
Their own research relatively new technology, say the results!
Comments: Well, highlight their potential, everyone free to play!
Do you have any questions? I just asked about the company over there, and this is free play!
Comment: ask about the job content, do not ask what benefits ah, overtime! This kind of question, do not ask in the technical aspect!
The last
Welcome to pay attention to my public number [programmer chasing wind], the article will be updated in it, sorting out the data will be placed in it.