After three months of intense review, Autumn Recruitment finally entered Baidu with a 30*15 salary, and the above sharing is hoped to help everyone

When you started to go to job-hopping interviews, it was obviously only a 15K job, but you were asked if you would have multi-threaded, and if you understand high concurrency, the rocket made you off guard, and the result is cool; nowadays Market, multi-threading, high-concurrency programming, distributed, load balancing, clustering, etc. can be said to be the necessary skills for advanced back-end development.
Insert picture description here
I have compiled all the information about Java basics, Spring, multithreading, high-concurrency programming, distributed, load balancing, clustering, etc. If you need it, click here to get the secret code CSDN!
Insert picture description here

                                     百度一面

The difference between String/StringBuffer and StringBuilder

  1. String is a class modified by final, and it is composed of byte(9+) or char(8-) arrays, these arrays are also final

  2. StringBuffer is thread-safe, StringBuilder is thread-unsafe. StringBuffer is implemented through the synchronized method level

  3. For StringBuffer and StringBuilder, they have a common parent class, namely AbstractStringBuilder, their properties and classes are not finalized, so they are mutable. Relatively speaking, StringBuffer has its own cache to ensure the performance of the query. This cache is not available in the builder.

Interface and abstract class

  1. Before JDK5, there was a significant difference between interfaces and abstract classes at the grammatical level: interfaces cannot have their own method bodies, and interfaces can only be public; abstract classes can have their own methods, and abstract classes can also have empty methods.

  2. With the upgrade of JDK, interfaces can have default methods in Java 8, and after Java 9, interfaces can also have their own private methods. Except that the properties are public final by default, the interface is almost indistinguishable from the abstract class at the syntax level.

  3. So for the difference between the two, we have to stand at a higher angle and look at the difference between them from the design level. In my opinion, the design of the interface is top-down, and the design of the abstract class is bottom-up. The template method pattern in the design pattern is a better embodiment of using abstract classes.

lock

  1. Locks in Java are actually divided into two categories, one is pessimistic lock and the other is optimistic lock. Pessimistic lock refers to the synchronized family, which includes Object#notify(), Object#notifyAll() and Object#wait() when used. Optimistic lock refers to a series of locks extended from the CAS package, including ReentrantLock and ReentrantReadWriteLock in JUC, which are used in conjunction with Condition

  2. For Synchronized, its lock granularity is at the object level. The default is a Class object, or it can be an instance object we specify. When modifying the method, it will be indicated as the ACC_SYNCHRONIZED logo in the flags of the bytecode, which indicates that the method is a synchronous method. The JVM uses the ACC_SYNCHRONIZED access flag to distinguish whether a method is declared as a synchronous method. Need to obtain the monitor lock first. When modifying the code block, it will be locked in the bytecode by executing monitorenter and monitorexit. When the thread executes to the monitorenter, the lock must be acquired before the subsequent method can be executed. Synchronized has made a series of optimizations with the evolution of the JDK, such as lightweight locks, lock coarsening, lock elimination, spin locks, and so on.

  3. For ReentrantLock, it is a reentrant lock. It corresponds to two internal classes representing fair locks and unfair locks, both inherited from Sync, and Sync inherits from AQS. For tryAcquire() of fair locks, it has one more judgment than unfair locks! HasQueuedPredecessors(), that is, if the current thread is at the top of the queue or the queue is empty, it will get the lock

  4. Compared with Synchronized, ReentrantLock requires manual acquisition and release, supports fair lock, selective notification and other functions.

About the collection, the concurrency problem of hashMap, the location when the key is NULL in the HashMap

  1. The collection in Java is divided into two parts, one is the collection under the java.util package, including list, vector, map, set, etc., and the other is the concurrent collection under the JUC package

  2. For list, it is divided into array storage and linked list storage, which are ArrayList and LinkedList (there are also vectors in array storage, but it is generally not used because of its low efficiency). For Map, it is divided into HashMap, WeakHashMap, TreeMap and LinkedHashMap. For HashMap, it uses the zipper method to solve the problem of hash conflict; for TreeMap, it sorts keys by red-black trees; for LinkedHashMap, it records the order of insertion of each key through a linked list, which can be It implements the LRU algorithm; for WeakHashMap, it is similar to the Entry in ThreadLocal, which inherits WeakReference. When the key is not referenced, it can be deleted in the next GC; for Set, several of its derivatives The classes are implemented by the corresponding Map

  3. When HashMap was in version 1.7, when the Entry array was expanded in multiple threads, because the header interpolation method was used, a circular singly linked list would appear, resulting in an infinite get loop problem (fixed in 1.8). In addition, concurrent put elements may cause coverage problems.

  4. The key in HashMap is null, the result of hash is 0, it will be saved in entry[0]

NIO 和 BIO

  1. IO reuse model NIO (select, poll, epoll): select used before in Java NIO, and now used epoll. After the user sends an IO request, there is a select thread to manage these requests (socket), it will continue to block the polling kernel whether the data about an event can be prepared, and if the event is not ready, it will always block. It is suitable for the situation with a large number of connections; select is based on the long array, which consumes a lot of kernel and user mode copy, and has a limit of 1024; poll is based on a linked list, and there is no size limit; epoll is based on map and is notified through events. Whenever fd is ready, the system The registered callback function will be called, and the ready fd will be placed in the ready queue. The time complexity is O(1). It should be noted that: the multiplexed IO model detects whether an event arrives by polling, and Respond to the arriving events one by one. Therefore, for the multiplexed IO model, once the event response body is large, it will cause the subsequent events to be delayed and will affect the new event polling.

  2. Java supports 3 types of IO so far, namely BIO, NIO and AIO. BIO corresponds to the blocking IO of the OS, NIO corresponds to the multiplexing model in the OS, and AIO is the asynchronous IO model.

  3. Blocking IO model BIO: When a user thread issues an IO request, the kernel will check whether the data is ready, if it is not ready, it will wait for the data to be ready, and the user thread will be blocked, and the user thread will hand over the CPU. When the data is ready, the kernel will copy the data to the user thread, and return the result to the user thread, the user thread will release the block state, such as socket.accept()

Thread state and transition

  1. There are six basic states of thread, namely NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED

  2. RUNNABLE: Divided into running state and ready state. The ready state is waiting for some resources such as CPU

  3. BLOCKED: Waiting for the monitor's lock, usually to enter the thread in the synchronized block or method. This corresponds to the _EntryList queue of the MonitorObject of the synchronized object

  4. WAITING: When the thread calls the Object.wait(), Thread.join() or LockSupport.park() method, it enters the waiting state until notify()/notifyAll(), LockSupport.unpark() or the thread calling the method End

  5. TIMED_WAITING: is a special waiting state, when the thread calls Thread.sleep(long), Object.wait(long), Thread.join(long), LockSupport.parkNanos(long) or LockSupport.parkUntil(long) method , Enter the timeout waiting state

  6. TERMINATED: The thread has completed execution and entered the end state

  7. NEW: The thread is new, but the start method is not called

Thread Pool

  1. Thread pool is a pooling technology, which reduces resource consumption, improves response speed, and improves ready-made manageability

  2. The thread pool has seven parameters, namely corePoolSize, maxmumPoolSize, keepAliveTime, unit, workQueue, threadFactory, and handle. When tasks come, the number of threads in the thread pool will gradually increase to core, and then put the extra in the queue. If Exceed the queue length, increase the number of thread pools to max, if it continues to increase, reject it through the handle. When the thread is idle for time, it will be destroyed to the number of cores.

  3. JUC provides four common thread pools, which are fixed (execute a fixed number of threads), single (execute a single task), cache (execute multiple short-term tasks) and scheduled (execute periodic tasks). The first two waiting queues are infinitely long, and the last two maximum threads are infinite

Cache coherency

  1. As long as there is a cache, there is a cache consistency problem

  2. Update the database first, then update the cache: If A updates the database, then B updates the database, then B updates the cache, and then A updates the cache, resulting in an inconsistency between the database and the cache; the same is true if you update the cache first, and then update the database. problem

  3. Delete the cache first, and then update the database: If A deletes the cache, then B goes to check the database, B fills the cache with dirty values, and A updates the database. At this time, the cache already has a value, causing the cache and the database to be inconsistent

  4. The delayed double deletion scheme is adopted, that is, the cache is deleted first, the database is updated, and then the cache is deleted after a delay. When the cache deletion fails, the failed key can be sent to the message queue and consumed again

Merging of ordered arrays

                                          百度二面

Database index

  1. For MySQL, the index is a B+ tree structure. The binary search tree ensures that the index is ordered. The balanced binary tree ensures that the index will not degenerate to the extreme case of the linked list. The B tree reduces the strictness of the balanced binary tree and improves In order to improve the construction efficiency, the B+ tree puts the value in the leaf node, and the tree becomes shorter, which improves the IO efficiency

  2. In addition to this B+ tree, there are Hash index, full-text index, and R-Tree index. You can create a business-oriented adaptive Hash index through the B+Tree index (this Innodb also has certain optimizations, among which Tencent also contributed some optimized code in November. For details, please see Tencent Technology Engineering Public Number# One is about to write MySQL source code The official bug resolution road)

  3. Indexes also have some concepts such as: clustered index, composite index, primary key index, index pushdown, unique index, leftmost principle, covering index, prefix index

  4. Some index failure scenarios include: like violates the leftmost principle, type does not match, use! Wait

AQS

  1. AQS uses the built-in FIFO double-ended doubly linked list to complete the queuing work of the resource thread, the double-ended doubly linked list. The queue is composed of Node nodes one by one, and each Node node maintains a prev reference and a next reference, pointing to its own predecessor and successor nodes respectively. AQS maintains two pointers, pointing to the head and tail of the queue respectively. The Node in the queue has five states, namely CANCELLED, SIGNAL, CONDITION, PROPAGATE and initial state

  2. In terms of usage, AQS can be divided into two functions: exclusive (such as ReentrantLock) and shared (such as Semaphore/CountDownLatch, CyclicBarrier/ReadWriteLock). ReentrantReadWriteLock can be regarded as a combined type, which is shared for read and exclusive for write

  3. In addition to ReentrantLock, there are three commonly used AQS components, namely Semaphore, CountDownLatch and CyclicBarrier

The field is too long, why not create an index

  1. If the field is too long, the storage of the index will become very large, and the search will also reduce the efficiency

  2. So we might as well use prefix index to reduce the length of the index as much as possible

  3. Write an LRU, how to write concurrent LRU

                                       百度三面
    
  4. Distributed lock

  5. For a stand-alone lock, it is just a mark that all threads/processes can see. For Java, synchronized is to set a mark in the object header. The implementation class of the Lock interface is basically just a volitile-modified int variable, which ensures that each thread can have visibility and atomic modification of the int; for linux In the kernel, it also uses memory data such as mutex or semaphore for marking.

  6. For the distributed situation, as long as we can find a public mark that can be accessed by all machines, we can use this mark to complete the nature of the distributed lock. We can put this mark in public memory, such as Redis, Memcache, or on disk, such as a database;

  7. With the mark, we have to consider the nature of this distributed lock, such as whether it is reentrant, fair, or blocking; consider the high availability and high performance of this distributed lock; consider the implementation of this distributed lock, such as optimistic Locks, pessimistic locks, etc.

  8. Can be based on the primary key and version number of the database, Redis SETNX (), EXPIRE () method for distributed lock, you can also use Zookeeper to build a distributed lock

Cluster discovery

Many middleware designs will use Zookeeper as the discovery mechanism for group members in the cluster. The client will establish a temporary directory on Zookeeper, and Zookeeper will establish a long connection with the client and send heartbeats regularly. Once the client is found to be inactive, Zookeeper will delete the temporary node established by the current client and send the message to the listener. This feature of Zookeeper is used as a cluster discovery mechanism by many middleware applications. For example, kafka uses Zookeeper to maintain the status of brokers and consumer groups, Hbase uses Zookeeper to elect the master of the cluster, and dubbo uses Zookeeper to maintain the survival status of each service instance (this is imported from the Internet, because I did not understand the meaning of the interviewer, so this The question is actually skipped)

The message from the broker to the consumer is not lost

  1. The message flow of MQ is divided into producer->broker->consumer. To ensure that the message is not lost, it is necessary to ensure the reliability of these two delivery processes. The unstable factors faced are network abnormalities, node downtime and so on.

  2. RocketMQ uses the ack mechanism. If the consumer fails to consume the message, the broker will deliver the message repeatedly (the number of delivery can be customized) until the consumption is successful, and the idempotence of the consumer interface must be guaranteed here; at the same time, the consumer itself maintains a persistent offset, mark the subscript that has been successfully sent back to the broker. Through this subscript, even if the Consumer is down, you can pull messages from the MessageQueue after restarting

Two realizations and reasons of aop

  1. Spring AOP uses jdk dynamic proxy and cglib. If the successor has an interface, use the dynamic proxy of JDK, otherwise, use CGLIB

  2. The dynamic proxy of JDK needs to implement a public interface (a method to find the proxy through the interface), the reflection class generated by the dynamic proxy. Proxy is a specific proxy. After we implement InvocationHandler, the invoke method will enter the Proxy (inherited Proxy and implements the interface) method

  3. CGLib is implemented by creating a subclass that inherits the implementation class and dynamically modifying the code of the subclass with the asm library, so you can use the passed-in class reference to execute the proxy class

                                      后记
    

From the perspective of the three rounds of interviews, the interviewers ask different questions depending on their level, but the algorithm is always the same. I suggest you brush LeetCode.

Throughout the autumn recruitment, I interviewed many domestic Internet companies, and I experienced about 30+ interviews. In the coming days, I will slowly publish these notes for everyone to prepare for spring recruitment and internships. Actually, I haven't had an interview for about three months, and I don't know some of the above questions, so I still warn us to keep learning!

In fact, I plan to organize my interview posts into PDF in the follow-up. It is roughly divided into four modules: interview steps, common interview questions, my interview experience, must brush algorithm, and OFFER selection. Hope it won't

Summarized some interview questions for 2020. The modules included in this interview question are divided into 19 modules, namely: Java Basics, Container, Multithreading, Reflection, Object Copy, Java Web, Exceptions, Network, Design Patterns, Spring /Spring MVC, Spring Boot/Spring Cloud, Hibernate, MyBatis, RabbitMQ, Kafka, Zookeeper, MySQL, Redis, JVM.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47345084/article/details/111473860