[java] basic content (4)

Briefly describe the java memory model (JMM)

The java memory model defines the access rules for various variables in the program. It stipulates that all variables are stored in main memory, and threads have their own working memory. The main memory copy of the variables used by the thread is saved in the working memory. All operations on the variables by the thread must be performed in the working space, and the main memory data cannot be directly read and written. After the operation is completed, the working memory of the thread flushes the operated data back to the main memory through the cache consistency protocol.

Brief description of as-if-serial

Compilers and the like perform instruction reordering and optimization on the original program. But no matter how they are reordered, the result is consistent with the predetermined result output by the user's original program. 

Briefly describe the eight principles of happens-before

Program order rule: the operations written in the front of a thread happen before the operations in the back.

Locking rules: The unlock operation happens before the lock operation on the same lock.

The volatile rule: A write operation to a volatile variable happens before a subsequent read operation.

Thread start rule: The start method of the thread occurs first in every action of the thread.

Thread Interruption Rules: The call to the thread interrupt() method happens before the code of the interrupted thread detects the occurrence of an interrupt event.

Thread termination rule: All operations in a thread happen before the termination detection of the thread.

Object finalization rules: Object initialization occurs first in the finalize method.

Transitivity rule: If operation A happens-before operation B, and operation B happens-before operation C, then operation A happens-before operation C

The difference between as-if-serial and happens-before

as-if-serial ensures that the execution results of single-threaded programs remain unchanged, and happens-before ensures that the execution results of correctly synchronized multi-threaded programs remain unchanged. 

 Briefly describe the visibility of threads

Visibility means that when a thread modifies a shared variable, other threads can immediately know the modification. volatile, synchronized, final can guarantee visibility.

Implementation of java thread

  1. Implement the Runnable interface
  2. Inherit the Thread class.
  3. Implement the Callable interface

Briefly describe the role of the volatile keyword in java

  1. Guarantees the visibility of variables to all threads. When a thread modifies the value of a variable, the new value is immediately available to other threads.
  2. Disable instruction reordering optimizations. Use volatile variables for write operations, and the assembly instructions are prefixed with lock, which is equivalent to a memory barrier, and the compiler will not rearrange the following instructions before the memory barrier.

 Briefly describe Lock and ReentrantLock

Lock is the top-level interface of java concurrent package.

ReentrantLock is the most common implementation of Lock, reentrant as synchronized. ReentrantLock is unfair by default, and a fair lock can be specified through the constructor. Once fair locks are used, performance will degrade.

 Briefly describe the JVM memory model

Thread-private runtime data area: program counter, Java virtual machine stack, native method stack.

Runtime data area shared by threads: Java heap, method area.

 Memory overflow and memory leak

Memory overflow: When the program is applying for memory, it has used too much memory at this time, and there is not enough remaining memory space for it to use.

Memory leak: After the program applies for memory, it cannot completely release the allocated memory space.

inversion of control (ioc) 

IoC is the core content of the Spring framework . IoC is perfectly implemented in a variety of ways, including XML configuration and annotations. The new version of Spring can also implement IoC with zero configuration.

When the Spring container is initialized, it first reads the configuration file, creates and organizes objects according to the configuration file or metadata and stores them in the container, and then takes out the required objects from the Ioc container when the program is used.

This process is called inversion of control:

  • Control: who controls the creation of objects, the objects of traditional applications are created by the program itself, after using Spring, the objects are created by Spring
  • Inversion: The program itself does not create objects, but becomes a passive recipient of objects.

Dependency injection: is to use the set method to inject.

IOC is a programming idea, from active programming to passive reception

aspect oriented programming (aop)

AOP: To put it bluntly, it is to automatically execute a series of custom statements before or/and after calling a certain method

The execution flow of springmvc

1. The user sends a request to the front controller DispatcherServlet.

2. DispatcherServlet receives a request to call HandlerMapping processor mapper.

3. The processor mapper finds the specific processor (can be searched according to the xml configuration and annotation), generates the processor object and the processor interceptor (if there is one), and returns it to the DispatcherServlet.

4. DispatcherServlet calls HandlerAdapter processor adapter.

5. The HandlerAdapter calls a specific processor (Controller, also called a back-end controller) after adaptation.

6. After the execution of the Controller is completed, it returns to the ModelAndView.

7. HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet.

8. DispatcherServlet passes ModelAndView to ViewReslover view parser.

9. ViewReslover returns a specific View after parsing.

10. DispatcherServlet renders the view according to the View (that is, fills the model data into the view).

11. DispatcherServlet responds to the user.

What are microservices

To put it simply, microservices are very small services, so small that a service only corresponds to a single function and does only one thing. This service can be deployed and run independently, and services can interact with each other through RPC . Each microservice is developed, tested, deployed, and launched by an independent small team, responsible for its entire life cycle. 

 restful style

A software architectural style, design style, rather than a standard, just provides a set of design principles and constraints. It is mainly used for client and server interaction software. Software designed based on this style can be more concise, more layered, and easier to implement mechanisms such as caching.

Principle of automatic assembly

  1. SpringBoot gets the value specified by EnableAutoConfiguration from META-INF/spring.factories under the classpath at startup

  2. Import these values ​​into the container as an automatic configuration class, and the automatic configuration class will take effect, helping us with the automatic configuration work;

  3. The overall solution and automatic configuration of the entire J2EE are in the jar package of springboot-autoconfigure;

  4. It will import a lot of automatic configuration classes (xxxAutoConfiguration) into the container, that is, import all the components needed for this scene into the container, and configure these components;

  5. With the automatic configuration class, it saves us the work of manually writing configuration injection function components, etc.;

All automatic configurations of springboot are scanned and loaded at startup: all automatic configuration classes of spring.factories are in it, but they may not take effect. To judge whether the conditions are true, as long as the corresponding start is imported, there will be corresponding startup With the starter, our automatic configuration will take effect, and then the configuration is successful.

Guess you like

Origin blog.csdn.net/weixin_46601559/article/details/126467913