concurrent package

atomic: jdk1.5 provides lock-free atomic operations in a multi-threaded environment,

The bottom layer uses the atomic instructions provided by the processor. The atomic instructions provided by different CPU architectures are different, and may also provide some form of internal lock, which cannot guarantee that the complete thread is not blocked.

 

1 12 classes, four atomic update methods, (classes in the atomic package are all wrapper classes implemented using Unsafe)

    Atomic update primitive types: update primitive types atomically: AtomicInteger AtomicLong AtomicBoolean etc.

    Atomic update array

    Atomic update quote

    Atomic update field

AtomicInteger   

        Unsafe , valueOffset , what is it? ?

        cas method? ? ? ? : compareAndSwapObject compareAndSwapInt etc.

        data structure, value of type int with volatile  

        1 get method: return value value

        2 set method: set a value

       3 lazySet method; call unsafe.putOrderedInt

       4 getAndSet ,调用unsafe.getAndSetInt

 

         addAndGet atomically adds the input number to the instance's value and returns the result

        compareAndSet Atomically sets the input value to the unentered value if the input value == the expected value 

        getAndIncrement Atomically increments the current value by 1, returning the value before the increment 

        int getAndSet sets the new value, returns the old value (and getAns)

 

 

 AtomicReference atomic type reference class:

 

 

 

public static void main(String[] args) {

 

 

 

 

 

 

 

Spring boot startup mechanism:

 

1 Annotate @SpringBootApplication and SpringApplication.run

 

SpringBootApplication   是

@Configuration : Spring ioc container configuration class in the form of javaconfig (registered bean, dependency injection relationship, equivalent to the work of the <bean> tag in the xml file)

@EnableAutoConfiguration : This type of annotation collects and registers the definitions of beans related to specific scenarios with the support of the @Import annotation

              That is, to load the bean definition that meets the automatic configuration conditions into the ioc container, the import annotation here uses the EnableAutoConfigurationImportSeletor 

            Load all eligible @Configuration configurations into spring's ioc container,

 

@ComponentScan : Automatically scan and load eligible components, or bean definitions, and finally load them into the spring ioc container,

The scope of fine-grained custom scanning can be specified through basePackages, and the default is to scan under the package path of the class where the annotation is located.

With the support of SpringFactoryiesLoader, the annotation can be automatically configured intelligently

 It is an extension scheme based on the spring framework, the main function is to load the configuration from the specified configuration file META-INF/spring.factories 

Used in conjunction with EnableAutoConfiguration, it provides functional support for configuration lookup, and obtains a set of classes with @Configuration based on its fully qualified class name as the key

That is: By finding the META-INF/spring.factorties configuration file under the classpath path,

Obtain the class name of the corresponding configuration class by using org.springframework.boot.autoconfigure.EnableutoConfiguration as the corresponding configuration item of the key.

Through the java reflection mechanism, instantiate the corresponding configuration class annotated with @Configuration annotation in the ioc container, and load it into the ioc container 

 

 

 

In-depth principle: 

The main process of the run method of spring boot: 

The static method run of SpringApplication first creates a SpringApplication object instance,

Then the method of the created instance object is called, and the instance is initialized as follows:

  1 According to whether there is a feature class in the classpath, decide whether to create a web application using the ApplicationContext type --- ConfigurablewebApplicationContext

2 Use SpringFactoriesLoader to find and load all available ApplicationContextInitializers in the application's classpath 

3 Use SpringFactioriesLoader to find and install all available ApplicationListeners in the application's classpath

4 Infer and set the defining class of the main method

 

2 After the initialization of the springApplication instance is completed and the settings are completed, the logic of the run method starts to be executed.

SprinClassLoader can find and load springApplicationRunListenter , and call their started method to tell springApplicationRunListener

The spring boot application is about to start executing

 

3 Create and configure the environment that all current spring boot applications will use

4 Traverse and call all the environmentPrepared methods of springbooRunListener to tell the listener that the springboot application is ready to use Environment

5 If springapplication's showBanner is set to true, print banner

6 According to whether the user has explicitly set the applicationContextClass type and the inference result of the initialization phase, it is determined that the current spring boot application is

What type of ApplicationContext to create and complete, and then decide whether to add showdownHook based on conditions

Decide whether to use a custom BeanNameGenrator, decide whether to use a custom ResoureLoader, of course, the most important thing is to set the previously prepared environment

Used for the created ApplicationContext.

7 After the ApplicationContext is created, it will find and install all available ApplicationContext-Initializers in the classpath with the help of springFacitoriesLoder again

Then traverse the initialize method of these ApplicationContextInitializer to further process the created ApplicationContext.

8 Traverse the contextPrepered method of calling all SpringApplicationRunListener

9 The core step is to load all the configurations previously obtained through @EnableAutoConfiguration and other forms of ioc container configurations into the prepared ApplicationContext

10 Traverse the contextLoaded method of calling all SpringApplicationRunListener

11 Call the refresh method of ApplicationContext to complete the last process of the ioc container  

12 Find out if there are CommandLineRunner registered in the current ApplicationContext, if there is a traversal to execute them

13 Traverse the finished method of springApplicationRunListener under normal circumstances (an exception occurs

If there is an exception in the whole process, the finished() method of all SpringApplicationRunListener will still be called, but in this case, the exception information will be passed in for processing)

After removing the event notification point, the whole process is as follows:

The concept of spring framework:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324939574&siteId=291194637