Ant Financial, the real interview question of the big factory, has been verified by many insiders! It is recommended to collect and watch repeatedly! (One)

In the future, we will update the real question of Ant Financial Service II of Dachang Interview

1. What has changed from jdk1.7 to jdk1.8 Map (underlying)?

After 1.8, the data structure of hashMap has changed, from the previous simple array + linked list structure to an array + linked list + red-black tree. That is to say, when the JVM stores the KV of the hashMap, only the key determines the storage slot of each entry (the index in Node[]). And Value is hung on the corresponding slot in the form of a linked list (after 1.8, if the value length is greater than 8, it will be converted to a red-black tree).
However, there is no synchronization operation in hashmap1.7 and 1.8, which is prone to concurrency problems, and even an infinite loop makes the system unavailable. The solution is jdk's ConcurrentHashMap, located under java.util.concurrent, specifically to solve concurrency problems.

2. ConcurrentHashMap

The idea is similar to hashMap, but it supports concurrent operations, which is much more complicated

3. What is the difference between parallel and concurrency?

Concurrency: Refers to applications that perform different tasks alternately, and the principle of multithreading.
Parallel: Refers to applications that perform different tasks at the same time.
Difference: One is alternate execution and the other is simultaneous execution.

4. What happened to the java virtual machine from jdk1.7 to jdk1.8?

The memory in JVM is heap, stack memory, and method area.
The main purpose of the stack memory is to execute thread methods, store local temporary variables and the addresses of reference objects needed for thread method execution.
The main purpose of heap memory: all object information in JVM is stored in heap memory. Compared with stack memory, heap memory is much larger. Therefore, JVM has been dividing the heap memory into different functional blocks to manage objects in heap memory.
Insufficient heap memory. Common error: OutOfMemoryError
Stack memory overflow. Common error: StackOverFlowError. In JDK7 and its earlier JDK versions, heap memory is usually divided into three areas: Nursery memory (young generation), long-term memory (old generation), and permanent memory (Permanent Generation for VM Matedata), as shown below:
Insert picture description here

At the top layer is Nursery memory. After an object is created, it is first housed in Eden memory in Nuersery. If the survival period exceeds two Survivors (lifetime periods), it will be transferred to Old Generation.
Metadata information such as object methods and variables are stored in permanent memory. If the permanent memory is not enough, the following error will occur: java.lang.OutOfMemoryError: PermGen
but this error is generally not obtained in JDK1.8, because: 1.8 the permanent memory for storing metadata has been changed from the heap memory to the local memory In (native Memory), the JVM memory structure in 1.8 becomes the following figure:
Insert picture description here

In this way, permanent memory does not occupy heap memory, and permanent memory errors can be avoided through self-growth.
-XX:MaxMetaspaceSize=128m This is the largest far memory space of 128 megabytes.
JDK1.8 removes PermGen and replaces it with MetaSpace source space.
MetaSpace Garbage Collection: Garbage collection mechanism for dead classes and class loaders is high in metadata usage Run at the setting value of the "MaxMetaSpaceSize" parameter.
MetaSpace monitoring: The usage of meta space can be obtained from the detailed GC log output of HotSpot 1.8.

Reasons for updating JDK1.8:
1. Strings exist in the permanent generation, which is prone to performance problems and memory overflow
. 2. It is difficult to determine the size of the class and method information, so it is difficult to set the size of the permanent generation, and it is easy to appear if it is too small Permanent generation overflow, too large will easily lead to overflow of the old generation.
3. The permanent generation will bring unnecessary complexity to the GC, and the recovery efficiency is low.
4. Oracle may want to merge HotSpot with JRockit.

5. If you were asked to design a middleware by yourself, how would you design it?

I will consider development from the following aspects:

  1. Remote procedure call
  2. Message-oriented: Use funny message transfer mechanism for platform-independent data exchange, and give data communication to integrate distributed system. There are three characteristics:
    i) Communication program can run at different times
    ii) Communication Chen Xuzhi Home can be one-to-one, one-to-many, many-to-one or even a mixture of the above methods
    iii) The program puts the message in the message queue, and the message is taken out of the small drug column for communication
  3. Object request agent: Provides different forms of communication services including synchronization, queuing, subscription publishing, broadcasting, etc. Various frameworks can be constructed such as transaction processing monitor, distributed data access, object transaction manager OTM, etc.
  4. Transaction processing monitoring has the following functions:
    a) Process management, including starting the server process, assigning tasks, monitoring its execution, and balancing the load
    b) Transaction management to ensure the atomicity, consistency and independence of transaction processing under its monitoring And persistence
    c) Communication management, which provides a variety of communication mechanisms between client and server, including request response, conversation, queuing, subscription publishing and broadcasting, etc.

6. What is middleware?

Middleware is the software between the operating system and applications. When used, Wangwang is a group of middleware integrated together to form a platform (development platform + running platform). There must be a communication middleware in this group of middleware. That is, middleware = platform + communication. This definition also restricts that only those who have the courage to be called middleware can be called middleware in distributed systems.
Main categories: remote procedure call, message-oriented middleware, object request agent, transaction monitoring.

7. Have you ever used ThreadLock, tell me what it does?

ThreadLock is a local thread and provides a local variable for each thread, which means that only the current thread layer can access it, which is thread-safe. Principle: Allocating an object for each thread to work is not done by ThreadLock, but needs to be guaranteed at the application level. ThreadLock only functions as a container. The principle is the set() and get() methods of ThreadLock.
Implementation principle:

public void set(T value) {
    
     
Thread t = Thread.currentThread(); 
ThreadLocalMap map = getMap(t); 
if (map != null) 
map.set(this, value); 
else 
createMap(t, value); 
} 
public T get() {
    
     
Thread t = Thread.currentThread(); 
ThreadLocalMap map = getMap(t); 
if (map != null) {
    
     
ThreadLocalMap.Entry e = map.getEntry(this); 
if (e != null) 
return (T)e.value; 
}
return setInitialValue(); 
}

8. What is the difference between Hashcode() and equals() and ==?

(1) The hashcode() method and equals() both judge whether two objects are equal in java.
(2) If the two objects are the same, the hashcode must be the same, that is, the objects are the same---->the member variables are the same---- -->Hashcode value must be the same
(3) The hashcode value of two objects is the same, and the objects are not necessarily equal. Summary: equals equals hashcode
must be equal, hashcode equals, equals is not necessarily equal.
(4) == compares whether two references point to the same object in memory (ie the same memory space)

9. In mysql database, under what circumstances are indexes set up but not available?

(1) The role of the index:

Indexing the fields in the database table can greatly improve the query speed.

(2) Mysql index type:

a)—Ordinary index
b)
—Unique index: The value of the unique index column must only allow null values. If it is a composite index, the combination of column values ​​must be unique: CREATE UNIQUE INDEX indexName ON mytable(username(length))
– modify Table structure
ALTER mytable ADD UNIQUE [indexName] ON (username(length))
– directly specify
CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16)
NOT NULL, UNIQUE [indexName] (username(length)) when creating the table );
c)—Primary key index: a special unique index that does not allow null values. Generally, create a primary key index when creating a table:
CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, PRIMARY KEY (ID) );
d) Composite index: CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, city VARCHAR(50) NOT NULL, age INT NOT NULL );
In order to further squeeze the efficiency of MySQL, it is necessary Consider building a composite index. Just build name, city, age into an index: the code is as follows:
ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age);

(3) Under what circumstances is there an index, but not useful?

a) If there is OR in the condition, it will not be used even if there are some conditions with index. Note: If you want to use or and want the index to take effect, you can only add an index to each column in the or condition.
b) For more indexes, not the first part used, the index will not be used.
c) Like query starts with% and does not use index
d) If there is an invisible conversion of the data type of the index column, no index is used. For example, if the column type is a string, then the data must be quoted in the condition, otherwise it will not Use index
e) There are mathematical operations on the index column in the Where clause, and the index is not used.
f) The function is used on the index column in the Where clause, but the index is not used.
g) Mysql estimates that using a full table scan is faster than using an index. No index

(4) Under what circumstances is it not recommended to use indexes?

a) Do not use indexes for fields with poor data uniqueness
b) Do not use indexes for frequently updated fields
c) Do not add indexes when fields do not appear in the where statement, if there is IS NULL/IS NOT NULL/LIKE'% input character%' after where Wait for conditions, do not use index
d) Use unequal (<>) for index in the Where clause, index is not recommended, the effect is general

10. Will mysql optimize, mycat sub-database, vertical sub-database, horizontal sub-database?

(1) Optimize your query for query caching
(2) EXPLAIN select query: The query result of explain will tell you how the index primary key is used
(3) Use limit1 when only one row of data is needed
(4) Add an index to the search field
( 5) Use a similar type of example when associating tables, and index them
(6) Never ORDER BY RAND()
(7) Avoid select*
(8) Always set an ID for each table
(9) Use ENUM Instead of VARCHAR
(10) extract suggestions from PROCEDURE ANALYS()
(11) use NOT NULL as much as possible
(12) use Prepared Statements in Java
(13) unbuffered queries
(14) save the IP address as UNSIGNED INT
(15) Fix the length of the table
(16) Vertical sub-database: "Vertical segmentation" is a method of turning a table in the database into several tables by column, which can reduce the complexity of the table and the number of fields, thereby achieving the purpose of optimization .
(17) Horizontal partitioning: "Horizontal partitioning" is a method of turning a table in a database into several tables by row, which can reduce the complexity of the table and the number of fields, thereby achieving the purpose of optimization.
(18) The smaller the column, the faster
(19) Choose the right storage engine
(20) Use an object-relational mapper
(21) Be careful with permanent links
(22) Split large DELETE or INSERT statements

11. Distributed transaction solution?

(1) What is a distributed transaction?

a. Under what circumstances need to use distributed transactions?
a) Distributed transactions are needed when the local database is powered off, the machine is down, the network is abnormal, the message is lost, the message is out of sequence, data error, unreliable TCP, stored data is lost, and other abnormalities.
b) For example: How to ensure data consistency when the local transaction database is powered off? The database is composed of two files, a database file and a log file. Any write operation to the database must first write the log. Before the operation, the log file will be written to the disk. Then it will be done in time when the power is off. When the database is restarted, the database will perform undo rollback or redo roll forward according to the current data situation, ensuring strong data consistency.
c) Distributed theory: When a single database performance bottleneck occurs, the database may be partitioned (physical partition). After partitioning, different databases are on different servers. At this time, the ACID of a single database is not suitable for this kind of hardship. In this cluster environment, it is difficult to achieve the ACID of the cluster, and even the efficiency performance is greatly reduced. The important thing is that it is difficult to expand new partitions. At this time, it is necessary to quote a new theory to use this clustering situation: CAP theorem
d) CAP theorem: Proposed by Professor Eric Brewer, a distributor of Berkeley, California, pointing out that WEB services cannot satisfy three attributes at the same time:
a. Consistency: customers The end knows that a series of operations will occur at the same time (effective)
b. Availability: Each operation must end with a predictable response
c. Partition fault tolerance: If a single component is unavailable in time, the operation can still be completed. Specifically, in a distributed system, in any database design, a WEB should only support the above two attributes at the same time. The designer must choose between consistency and usability.
e) BASE theory: The pursuit of availability in distributed systems is more important than consistency. BASE theory can achieve high availability. The core idea is: we cannot achieve hydroxyethyl ester, and each application alone can adopt an appropriate method to achieve the ultimate consistency of the system according to its own business characteristics.
f) Database transaction characteristics: ACID
i. Atomicity
ii. Consistency
iii. Independence or isolation
iv. Persistence

(2) In the distributed system, the solution to realize the distributed transaction:

a. Two-phase commit 2PC
b. Compensation transaction TCC
c. Local message table (asynchronous guarantee)
d. MQ transaction message e. Sagas transaction model

12. Will sql statement optimize, tell me what you know?

(1) Avoid doing calculations on columns, which may cause index failure.
(2) When using join, a small result set should drive a large result set. At the same time, the complex join query should be split into multiple queries, otherwise the more tables will be joined, Lead to more locks and jams.
(3) Pay attention to the use of like fuzzy query, avoid using %%
(4) Do not use select * to save memory
(5) Use batch insert statements to save interaction
(6) When the limit cardinality is relatively large, use between and
(7) Do not use The rand function obtains records randomly.
(8) Avoid using null. When creating a table, try to set not nul to improve query performance.
(9) Don’t use count(id), you should use count(*)
(10) Don’t do unnecessary sorting. Sorting may be done in the index
(11) Do not use sub-queries in the From statement
(12) Use more where to limit and narrow the search range
(13) Use the index reasonably
(14) Use explain to view SQL performance

13. Have you understood the storage engine of mysql?

(1) MySQL storage engine types:
Insert picture description here
(2) Transaction processing: Any problem in the entire process can make the data roll back to the initial state. This processing method is called transaction processing. In other words, the transaction processing either succeeds or fails.

14. Red-black tree principle?

(1) The nature of the red-black tree: the red-black tree is a binary search tree. A storage bit is added to each node to record the color of the node, which can be RED or BLACK. Through the color constraint on any simple path from root to leaf, the red-black tree ensures that the longest path does not exceed two of the shortest path. Times and balance. The properties are as follows:
i. The color of each node is either black or red
ii. The color of the root node is black
iii. If a node is red, then its two child nodes are black, and there is no continuous red node iv. For each A node, a simple path from the node to its descendant leaf nodes, contains the same number of black nodes.

Guess you like

Origin blog.csdn.net/Java_Yhua/article/details/111561456
Recommended