Some necessary flowcharts for Java programmers

Posted May 25, 20208 min read

Foreword:

Organized some basic Java flow charts/architecture diagrams, took notes, and everyone studied together.

1. The life cycle of spring

Spring is currently the most popular and powerful lightweight container framework in Java. It is necessary to understand the life cycle of Spring.

  • First, after the container is started, initialize the bean
  • Inject properties according to bean definition
  • Check whether the object implements the xxxAware interface, and inject the relevant xxxAware instance to the bean, such as BeanNameAware, etc.
  • In the above steps, the bean object has been correctly constructed. By implementing the BeanPostProcessor interface, you can perform some custom method processing.
    Such as:postProcessBeforeInitialzation.
  • After the pre-processing of BeanPostProcessor is completed, you can implement methods such as postConstruct, afterPropertiesSet, init-method,
    Add our custom logic,
  • PostProcessAfterInitialzation post-processing by implementing BeanPostProcessor interface
  • Then the Bean is ready to be used.
  • After the container is closed, if the Bean implements the DisposableBean interface, the destroy() method of the interface will be called back
  • By specifying a function to destroy-method, you can execute the specified logic before the bean is destroyed

2.TCP handshake three times, wave four times

TCP's three-way handshake and four-way wave are familiar to every programmer.

Three-way handshake:

  • The first handshake(SYN = 1, seq = x), after sending, the client enters SYN \ _SEND state
  • The second handshake(SYN = 1, ACK = 1, seq = y, ACKnum = x + 1), after sending, the server enters SYN \ _RCVD state.
  • The third handshake(ACK = 1, ACKnum = y + 1). After sending, the client enters the ESTABLISHED state. When the server receives this packet, it also enters the ESTABLISHED state. The TCP handshake starts data transmission.

Four waves:

  • The first wave(FIN = 1, seq = a), after sending, the client enters FIN \ _WAIT \ _1 state
  • The second wave(ACK = 1, ACKnum = a + 1), after sending, the server enters the CLOSE \ _WAIT state, and the client enters the FIN \ _WAIT \ _2 state after receiving the confirmation packet
  • The third wave(FIN = 1, seq = b), after sending, the server enters the LAST \ _ACK state, waiting for the last ACK from the client.
  • The fourth wave(ACK = 1, ACKnum = b + 1), the client receives the shutdown request from the server, sends a confirmation packet, and enters the TIME \ _WAIT state, waiting for a fixed time(two maximum periods) After the life cycle, 2MSL, 2 Maximum Segment Lifetime), no ACK is received from the server, and the server is considered to have closed the connection normally, so it also closed the connection and entered the CLOSED state. After receiving the confirmation packet, the server closes the connection and enters the CLOSED state.

3. Thread pool execution flowchart

Thread pool:A thread usage mode. Too many threads will cause scheduling overhead, which in turn affects cache locality and overall performance. The thread pool maintains multiple threads, waiting for the supervisor to allocate tasks that can be executed concurrently, which avoids the cost of creating and destroying threads when processing short-term tasks. The thread pool execution process is a must for every development.

Implementation process

  • Submit a task, when the number of core threads surviving in the thread pool is less than the number of threads corePoolSize, the thread pool will create a core thread to process the submitted task.
  • If the number of core threads in the thread pool is full, that is, the number of threads is already equal to corePoolSize, a newly submitted task will be placed in the task queue workQueue and wait for execution.
  • When the number of surviving threads in the thread pool is already equal to corePoolSize, and the work queue of the task queue is also full, determine whether the number of threads reaches the maximumPoolSize, that is, whether the maximum number of threads is full, and if not, create a non-core thread to execute the submitted task.
  • If the current number of threads reaches the maximumPoolSize, and there is a new task coming, use the rejection strategy directly.

JDK provides four types of rejection strategy processing

  • AbortPolicy(throw an exception, the default)
  • DiscardPolicy(discard tasks directly)
  • DiscardOldestPolicy(discard the oldest task in the queue and continue to submit the current task to the thread pool)
  • CallerRunsPolicy(handled to the thread where the thread pool is called for processing)

4. JVM memory structure

The JVM memory structure is the foundation that Java programmers must master.

Program counter(PC register)

The program counter is a small memory space, which can be regarded as the line number indicator of the bytecode executed by the current thread. In the model of the virtual machine, the bytecode interpreter works by changing the value of this counter to select the next bytecode instruction that needs to be executed. Basic functions such as branching, looping, exception handling, and thread recovery need to rely on the counter to complete .

Java Virtual Machine Stack

  • Like the program counter, the Java virtual machine stack is also private to the thread, and its life cycle is the same as the thread
  • When each method is executed, a "stack frame" is created, which is used to store local variable table(including parameters), operand stack, dynamic link, method exit and other information. Each method is called to execute the process, which corresponds to a stack frame in the virtual machine stack from the stack to the stack.
  • The local variable table stores various basic data types boolean, byte, char, short, etc.

Local method stack

Basically similar to the virtual machine stack, the difference is that the virtual machine stack serves the java method executed by the virtual machine, while the local method stack serves the Native method.

Java heap

  • The GC heap is the largest memory area in the memory managed by the java virtual machine, and is also the memory area shared by each thread, which is created when the JVM starts.
  • Its size is set by the -Xms(minimum) and -Xmx(maximum) parameters, -Xms is the minimum memory requested when the JVM is started, and -Xmx is the maximum memory that the JVM can apply for.
  • Since the collectors now use generational collection algorithms, the heap is divided into new generation and old generation. The young generation consists of S0 and S1, and the size of the young generation can be specified by the -Xmn parameter.
  • All object instances and arrays are allocated on the heap.
  • In addition to the class version, fields, methods, interfaces, and other description information in the Class file, there is a piece of information is a constant pool, used to store various symbol references generated by the compiler, this part of the content will be placed after the class is loaded The runtime constant pool of the method area.

Method area

  • Also known as "permanent generation", it is used to store class information, constants, static variables loaded by the virtual machine, and is a memory area shared by various threads. The size of the method area can be limited by the -XX:PermSize and -XX:MaxPermSize parameters.
  • Run-time constant pool:It is a part of the method area, the main content of which comes from the JVM loading Class.
  • In addition to the class version, fields, methods, interfaces, and other description information in the Class file, there is a piece of information is a constant pool, used to store various symbol references generated by the compiler, this part of the content will be placed after the class is loaded The runtime constant pool of the method area.

5. Java Memory Model

  • Java multi-thread communication is through shared memory, there will be a series of problems such as visibility, atomicity, sequence, etc. in the communication process, and JMM is around multi-thread communication and a series of related features And the established model. JMM defines some grammar sets. These grammar sets are mapped to the Java language, which are keywords such as volatile and synchronized. If you are interested, you can take a look at my other note: https://www.jianshu.com/p/3c1...
  • The Java memory model stipulates that all variables are stored in the main memory, and each thread has its own working memory. The working memory of the thread saves a copy of the main memory copy of the variables used in the thread. All operations must be performed in the working memory, not directly read and write main memory. Different threads cannot directly access the variables in the other party's working memory. The transfer of variables between threads requires data synchronization between their own working memory and main memory.

6.springMVC execution flowchart

  • User sends a request to the server, the front-end controls Servelt DispatcherServlet capture;
  • DispatcherServlet parses the request URL, calls HandlerMapping to obtain all related objects configured by the Handler, and finally returns in the form of HandlerExecutionChain object.
  • DispatcherServlet selects a suitable HandlerAdapter according to the obtained Handler.
  • Extract the model data in the Request, fill in the Handler input parameters, and start executing the Handler(Controller)
  • After the Handler is executed, return a ModelAndView object to DispatcherServlet
  • According to the returned ModelAndView, select a suitable ViewResolver
  • ViewResolver combines Model and View to render the view
  • Return the rendering result to the client.

7.JDBC execution flow

JDBC execution process:

  • Connect to data source
  • Pass query and update instructions for the database
  • Process database response and return results

8.spring cloud component architecture

Spring Cloud is a cloud-native application development tool based on Spring Boot. It is a configuration management, service discovery, fuse, intelligent routing, micro-agent, control bus, distributed session, and cluster involved in JVM-based cloud-native application development. Operations such as state management provide a simple way to develop.

  • Eureka is responsible for service registration and discovery.
  • Hystrix is responsible for monitoring the calling status between services, and plays a role in fusing and downgrading.
  • Spring Cloud Config provides a unified configuration center service.
  • All external requests and services are forwarded through Zuul, acting as an API gateway
  • Finally, we use Sleuth + Zipkin to record all the request data, which is convenient for our subsequent analysis.
  • Spring Cloud Ribbon is a set of client load balancing tools based on Netflix Ribbon.
    It is a client load balancer based on HTTP and TCP.
  • Feign is a declarative Web Service client. Its purpose is to make Web Service calls easier.

9.dubbo call

Dubbo is a distributed service framework, dedicated to providing high-performance and transparent remote service invocation solutions, which is easy to confuse with load balancing. Load balancing is to provide a public address to the outside. When the request comes, it will be polled, randomized, etc. To a different server.

  • Provider:Service provider that exposes the service.
  • Consumer:The service consumer who invokes the remote service.
  • Registry:Registry of service registration and discovery.
  • Monitor:A monitoring center that counts the call times and call times of statistical services.
  • Container:Service running container.

10. Follow-up to be updated ...

Personal public number

Everyone is welcome to pay attention and study and discuss together.