Java focus on multi-threading (thread state, thread concurrency, the difference between Synchronized and Lock and the underlying principle, common locking and its use scenarios and principles, volatile and ThreadLocal solve the problem, CAS in Java thread pool principle and implementation, Blocking queues and thread-safe queues, interthread communication: Synchronized + wait, notify/notifyAll, Lock + Condition multiplexing, CountDownLatch, CyclicBarrier and Semaphore function and usage, JVM memory management mechanisms and garbage collection mechanisms (memory model, GC policy, algorithm, generational GC type, Full GC, Minor GC scope, and trigger conditions) Design patterns (familiar with the application scenarios of common design patterns, able to draw class diagrams, commonly used: JAVA collection class framework (understand the relationship and differences between block diagrams, HashMap, ArrayList, HashSet, etc.) The process of rehash, the collision of Pointers, the thread-safety of HashMap, why it’s thread-safe the data structure of ConcurrentHashMap, the underlying principles, Put and GET are thread safe JAVA exception handling mechanisms (exception classification, common exceptions, Try catch finally use) JVM execution mechanisms (understand how the JVM works, Understand the class loading mechanism and the initialization order of classes.) Java NIO three main concepts Channel, Buffer, Selector, why improve performance? Pluses: Familiar with Netty Linux foundation (there are certain requirements for Linux in the interview written test, it is recommended to build a Linux VIRTUAL machine and practice common commands) framework Spring Spring IOC principle, Bean generation and life cycle (factory mode + reflection generation + singleton), How does Spring handle high concurrency? How to ensure performance under high concurrency? In Spring singleton mode, you can use ThreadLocal to switch parameters directly from one thread to another. ThreadLocal is used to ensure thread-safety. In fact, the key of a ThreadLocal is the Thread instance of the current Thread. In Spring’s singleton mode, the value of each Thread’s parameters that may have thread-safety problems is put into the ThreadLocal. However, data in different threads is isolated from each other, because there are far fewer beans created and destroyed at runtime, so in most scenarios this approach consumes less memory resources, and the higher the concurrency, the greater the advantage. Note: Spring MVC controllers are not thread-safe! Spring MVC is more granular with method-based interception, whereas Spring controllers default to Singleton, i.e. : Each request is handled by the same Controller. Both Spring MVC and Servlet are thread-safe at the method level. If there are instance variables in a singleton Controller or Servlet, it is thread-safe. Struts2 does have the advantage of being thread-safe: instead of creating controllers every time, object creation and destruction are reduced: Controllers are singletons, and variables in controllers are not thread-safe. Use ThreadLocal variables in Controller, encapsulate unsafe variables in ThreadLocal, use ThreadLocal to hold class variables, and store class variables in thread variable domain to isolate different requests 2. Scope =”prototype”; each request creates a new Controller. Transactional Transactional propagation properties declarative transaction management, adding the @Transactional annotation on top of services or methods of Services how does @Transactional work? When Spring starts, it parses and generates the associated beans. It looks at the classes and methods that have the associated annotations, generates a proxy for those classes and methods, and implements configuration injection based on the @Transactional parameter to process the Transactional transactions within the proxy. In the real database layer, transaction commit and rollback are implemented through binlog and redo log. Only Singleton scoped, setter-style loop dependencies are supported! Constructor mode and Prototype loop dependency are not supported. To create Bean A, we first create an instance of Bean A using the no-parameter constructor, the properties are empty, but the object reference has been created, and then we expose the reference to Bean A ahead of time. Then when we set the B property, we create the B object, and then we construct A reference to the B object using the no-parameter constructor. And expose the B object reference. B then executes the setter method to find A in the pool (because at this point, A has been exposed and has A reference to that object), so the dependency B is constructed and initialized, and then A is initialized, and the cyclic dependency is resolved! Summary: first create object reference, then use setter() to assign value to property, layer create object!! When Bean A is initialized, it first initializes its dependency B, and generates its own reference through the default no-argument constructor without calling its setter() method. When A B object is created, it also generates A reference to B through the no-argument constructor if C is also dependent on it. When C object is created, if A reference to A is used, After C is created, B uses setter() to inject C. After THE scene of B is created, A uses setter() to inject B. Finally, Complete setter() loop dependency! 1.InnoDB does not support FULLTEXT index. 2.InnoDB does not store the exact number of rows in a table, that is, when performing select count() from table, InnoDB scans the entire table to calculate the number of rows, but MyISAM simply reads the number of rows saved. Note that when the count() statement contains the WHERE condition, the operations on both tables are the same. 3. For AUTO_INCREMENT columns, InnoDB must contain an index only for that column, but in MyISAM tables, you can create a joint index with other columns. 4. When DELETE FROM table, InnoDB does not re-create tables, but deletes them row by row. 5.LOAD TABLE FROM MASTER does not work for InnoDB, the solution is to first change InnoDB tables to MyISAM, then change InnoDB tables after importing data, but it does not work for tables that use additional InnoDB features (such as foreign keys). In addition, InnoDB table row locks are not absolute. If MySQL cannot determine the range to be scanned when executing a SQL statement, InnoDB table locks all tables. For example, update table set num=1 where name like “%aaa%” None of the tables are universal. Select an appropriate table type for the service type to maximize MySQL’s performance advantages. Pessimistic and Optimistic locking (pessimistic locking: a real lock that allows only one thread to operate on the same record, optimistic locking: a collision detection mechanism, usually implemented by version number or timestamp, has little impact on performance) index usage and its indexing principle (Index underlying implementation: B+ tree) Query Query optimization 1. Explain SQL to view execution efficiency and locate performance bottlenecks of optimized objects 2. Always use small results to drive large result sets 3. Complete sorting in indexes whenever possible 4. Select only the columns you want, not * 5. Use the most efficient filter condition 6. Use table joins instead of subqueries 7. Use limit 1 8 when only one row is required. 9. Do not ORDER BY RAND(), avoid select * 10. Use NOT NULL if possible 11. Enable query cache and optimize query statement for query cache eg: select username from user where add_time >= now() note: 1. Such statements do not use query caching. 2. SQL functions such as NOW() and RAND() and others do not enable query caching because their returns are variable. So, all you need is to replace MySQL’s function with a variable to enable cache 3. Redis: now(); now(); yyyY-MM-DD: now(); Deposit in the province and jumping table: query faster) Redis as a distributed message queue to use, performance and pay attention to some common data structure and algorithm of sorting algorithms don’t say, you need to understand the principle and write code, space complexity and time also want to know the queue, stack: need to understand the access structure, and can be used in some scenarios Binary tree: Tree traversal, tree depth, output by level, balance binary tree, reverse print tree and other linked lists: reverse, merge two ordered linked lists, determine whether the list is a ring, the KTH element from the bottom of the list, etc. String: KMP algorithm, dynamic programming (this is the key, need to understand dynamic programming, common questions are: Solving the longest loop substring, solving the longest common substring, etc.) Now many large companies will ask about processing massive data, so you need to master common processing methods, such as bit-map, divide-and-conquer, hash mapping, etc. You can read relevant articles on Baidu. Common algorithms Bubble sort Quicksort Insert sort Hill sort merge sort heap sort Bucket sort dynamic programming longest common substring longest loop substring array maximum k value maximum sum of consecutive subarrays left rotation string String matching algorithm :KMP algorithm binary lookup linked list linked list reverse order Orderly singly linked lists to merge two crossings of the intersection of two singly linked lists whether singly linked list of nodes from the first K number singly linked lists sorted stack and queue design contains the min function of two queue implementation stack two implementation queue an array to realize the stack and queue, tree before the order, in sequence, the depth of the subsequent binary tree traversal o According to the level of traversing binary tree There are six principles of design mode to judge whether a binary tree is a complete binary tree to judge whether a binary tree is mirror symmetric to judge whether two trees are equal

  1. The single Responsibility principle (SRP) defines that there should be only one reason for a class to change.

It’s hard to understand from this definition what it means. In layman’s terms, we don’t want to give a class too much responsibility. If a class takes on too many responsibilities, these responsibilities are coupled together, and changes in one responsibility may impair or inhibit the class’s ability to perform other responsibilities. This coupling can lead to fragile designs that can be broken when changes occur. For example, I often see some Android developers write Bean files, network data processing, if there is a list of Adapter is also written in the Activity, ask them why other than easy to find there is no reason, wouldn’t split them into other classes easier to find. If the Activity is too bloated and has too many lines, it is obviously not a good thing. If we need to modify the Bean file, network processing and Adapter will need to modify the Activity, resulting in too many reasons for the Activity to change, and we will have a headache during version maintenance. This is a serious violation of the definition that “there should be only one reason for a class to change.” Of course, this pattern can cause a lot of debate if you want to, but remember, you’re not just writing code for yourself but for others.

  1. The Open Closed Principle (ASD) defines that classes, modules, functions, and so on should be extensible but not modifiable.

Open closed has two meanings, one is open to expansion, the other is closed to modification. Demand for development is certainly want to change, but the new demand, we will change the class to it’s clearly a headache again, so we design program in the face of demand change as far as possible to ensure that the relatively stable, try to change with new code implementation to expand demand, rather than by modifying the original code to implement. Suppose we want to achieve a list, only the function of the query at the beginning, if the product is added to increase the function, and to increase the delete function in a few days, most people approach is to write a method and then by passing in different values to control the way to implement different functions, but if have new features we have to modify our approach. The solution to the closed development principle is to add an abstract function class and subclass the abstract function class, so that if we add a function class, you will find that we don’t need to modify the original class, just add a function class subclass to implement the function class methods. 3. Definition of Richter’s Replacement Principle (LSP) : All references to base classes (parent) must be able to transparently use objects of derived classes without knowing its magnitude of substitution principle tells us that the software will be a base class object to replace its subclass object, the program will not produce any error and abnormal, which in turn is not established, if a software entity using a subclass object, then it is not necessarily to be able to use the base class object. Richter’s substitution principle is one of the important ways to realize the open and close principle. Because we can use the base class object everywhere, we can use the subclass object, so we try to use the base class type to define the object in the program, and then determine the subclass type at run time, and replace the parent class object with the subclass object. Note the following when using the Richter substitution principle: All methods of a subclass must be declared in the parent class, or a subclass must implement all methods declared in the parent class. According to the Richter substitution principle, in order to ensure the extensibility of the system, the parent class is usually used to define the method in the program. If a method only exists in the subclass and does not provide the corresponding declaration in the parent class, the method cannot be used in the object defined by the parent class. We are using the substitution principle on the Richter scale, as far as possible the parent class is designed for an abstract class or interface, let the subclass inherits the parent class or implement the parent interface, and implement methods declared in the parent class, run time, subclass instance to replace the parent class instance, we can easily extend the functionality of the system, at the same time, do not need to modify the original subclass code, Adding new functionality can be done by adding a new subclass. Richter’s substitution principle is one of the concrete means to realize the open – close principle. In the Java language, at compile time, the Java compiler checks whether a program complies with the Richter substitution principle. This is an implementation-independent, purely syntactic check, but the Java compiler’s check is limited. 4. The dependency Inversion principle (DIP) defines: high-level modules should not depend on low-level modules, and both should depend on abstractions. Abstraction should not depend on details, details should depend on abstractions. In Java, abstraction is an interface or abstract class, neither of which can be instantiated directly; Details are the implementation classes, and details are generated by implementing interfaces or inheriting abstract classes, which are objects that can be generated by adding the keyword new. The high-level module is the calling side, and the low-level module is the concrete implementation class. In Java, dependency inversion principle is expressed as follows: modules occur through abstraction, and implementation classes do not directly depend on each other. Their dependencies are generated through interfaces or abstract classes. If classes depend on details directly, they are coupled directly, and when they are modified, they change the dependent code at the same time, limiting extensibility. 5. The Demeter Principle (LOD) defines that a software entity should interact with as few other entities as possible. Also known as the least knowledge principle. If a system conforms to Demeter’s law, when one module is modified, other modules will be affected as little as possible and expansion will be relatively easy. This is the limitation of communication between software entities. Demeter’s law requires that the width and depth of communication between software entities be limited. Demeter’s law can reduce the coupling degree of the system and keep loose coupling relationship between classes. Demeter asked us when designing the system, should try to reduce the interaction between objects, if do not have to communicate directly with each other between the two objects, then the two objects should not be any direct interaction, if one of the objects you need to call a method of another object, you can through third party forwarding the call. In short, it is to reduce coupling between existing objects by introducing a reasonable third party. When applying Demeter’s law to system design, we should pay attention to the following points: in the division of classes, we should try to create loosely coupled classes, the lower the degree of coupling between classes, the more conducive to reuse, once a loosely coupled class is modified, it will not cause too much impact on the associated classes; In class structure design, each class should minimize the access permissions of its member variables and member functions; In class design, whenever possible, a type should be designed as an immutable class; An object’s references to other objects should be minimized to references to other classes. 6. The Interface Isolation Principle (ISP) defines that the dependency of one class on another should be based on the smallest interface. Create a single interface, do not create a large and bloated interface, try to refine the interface, interface methods as few as possible. That is, instead of trying to create a huge interface for all the classes that depend on it, we need to create interfaces that are specific to each class. When using the interface isolation principle to restrict interfaces, note the following: The interfaces should be small but limited. Refinement of interfaces can improve programming flexibility, but if it is too small, it can result in too many interfaces and complicate the design. So do it in moderation. Customizing the service for classes that depend on the interface exposes only the methods that the calling class needs and hides those it doesn’t. Only by focusing on providing custom services for a module can minimal dependencies be established. Improve cohesion and reduce external interactions. Make the interface do the most with the fewest methods. Because Java CAS has the memory semantics for both volatile reads and volatile writes, Java threads now communicate in the following four ways: Thread A writes A volatile variable, and thread B reads the volatile variable. Thread A writes the volatile variable, and thread B updates the volatile variable with CAS. Thread A updates A volatile variable with CAS, and thread B updates the volatile variable with CAS. Thread A updates A volatile variable with CAS, which thread B then reads. Java CAS will use modern processors provide efficient machine level atomic instructions, these atoms instruction atomically to memory read – to – write operations, this is the key to achieve synchronization in multiprocessor (in essence, can support atomic reading – to – writing instruction computing machines, calculate the Turing machine is order equivalent asynchronous machine, So any modern multiprocessor will support some kind of atomic instruction that can perform atomic read-modif-write operations on memory. Meanwhile, read/write of volatile variables and CAS enable communication between threads. Taken together, these features form the building blocks for the implementation of the concurrent package. If we look closely at the source code implementation of the Concurrent package, we find a common implementation pattern: First, declare the shared variable volatile; Then, atomic conditional update of CAS is used to achieve synchronization between threads. At the same time, the thread communication is realized with volatile read/write and CAS memory semantics. AQS, non-blocking data structure and the atomic variable classes (Java. Util. Concurrent. Atomic package’s classes), the concurrent is the base class in the package using this model, and the top class in the concurrent bag is dependent on the base class.

This article is from the cloud community partner “full stack Developer Community”. For relevant information, you can pay attention to “full stack Developer Community”.