laitimes

Comparison of several life cycles in Java

author:Speed Starry Sky 4DO

overview

The life cycle of a Java object consists of the following 6 phases:

  1. Created
  2. In Use
  3. Invisible phase
  4. Unreachable
  5. Collected phase
  6. Finalization

Create a stage

That is, the declaration and construction stage of the object, the main process is as follows:

  • Allocate space in memory
  • Start constructing the object
  • Static member initialization from superclass to subclass (loading static variables)
  • Calling constructors starting from superclasses (parental delegation mechanism)
  • Subclass member variables are initialized and subclass constructors are called

Once an object is created and assigned to certain variables, the state of the object switches to the application phase.

Application phase

The object is held by at least one strong reference

Invisible phase

That is, the object is out of scope (the object was created and referenced in a method, a method has been called, and the object has entered the invisible stage and can no longer be referenced)

Unreachable stage

An object is in the unreachable phase when it is no longer held by any strong references.

In contrast to the "invisible phase", the "invisible phase" is when the program no longer holds any strong references to the object, in which case the object may still be held by some loaded static variable or thread under a system such as the JVM or a strong reference such as JNI, these special strong references are called "GC root". The presence of these GC roots causes a memory leak condition of the object and cannot be reclaimed.

Collection phase

When the garbage collector finds that the object is already in the Unreachable phase and the garbage collector is ready to reallocate memory space for the object, the object enters the Collection phase. If the object has overridden the finalize() method, it will perform the method's terminal operation.

Finalization phase

When the object finishes executing the finalize method, it enters the unreachable state and waits for GC recycling

The lifecycle of the thread

Comparison of several life cycles in Java

overview

A thread's lifecycle consists of 6 states:

  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • Timer Waiting (TIMED_WAITING)
  • TERMINATED

When the thread enters the running state, the general operating system uses preemption (one of the two thread scheduling modes, the other is time-sharing scheduling) to let the thread obtain the CPU. Therefore, the CPU needs to switch between multiple threads, so the thread state will also switch between running, blocking, and ready many times.

NEW

USING THE NEW METHOD, THE NEW THREAD COMES OUT, WHERE ONLY THE JAVA VIRTUAL MACHINE ALLOCATES MEMORY FOR IT AND INITIALIZES THE VALUES OF THE MEMBER VARIABLES. At this time, the start method is not called, the program is not running, and the program enters the RUNNABLE state.

RUNNABLE

The Runable state in Java corresponds to two states in the operating system thread state, running (running) and ready (waiting for CPU to allocate resources), that is, a thread in the Runnable state in Java may be executing, or it may not be executing, waiting to be allocated CPU resources.

So, if a running thread is in the Runnable state, when it runs halfway through the task, the CPU executing the thread is scheduled to do other things, causing the thread to temporarily not run, its state remains unchanged, or Runnable, because it may be scheduled back at any time to continue executing the task.

BLOCKING STATE

DIFFERENT FROM THE RUNNABLE STATE, THERE ARE THREE OTHER SITUATIONS THAT CAUSE THREADS TO NOT RUN, NAMELY: BLOCKED (BLOCKED), WAITING (WAITING), TIMED_WAITING (TIMING WAITING), WHICH CAN BE COLLECTIVELY REFERRED TO AS BLOCKING STATE (BLOCKING)

BLOCKED

WHEN A THREAD IN THE RUNNABLE STATE GIVES UP THE USE OF THE CPU FOR SOME REASON (DOES NOT GET THE SYNC LOCK OR THE IO REQUEST IS BLOCKED), ENTERS THE BLOCKED STATE AND PAUSES THE OPERATION, THE JVM WILL NOT ALLOCATE THE CPU TO THE THREAD UNTIL THE THREAD RETURNS TO THE RUNNABLE STATE.

WAITING

Due to some conditions not being met, the running program actively calls methods such as Object.wait()/Thread.join()/LockSupport.park(), and the program enters the WAITING state (put into the object waiting pool) and waits for other threads to wake up.

Timer Waiting (TIMED_WAITING)

When calling the wait method, add the wait duration parameter, or call the sleep method, enter the timing wait, and automatically wake up by the system after the timeout, or you can be notified in advance

STATE OF DEATH (TERMINATED)

  • The run method finishes executing, and the thread exits normally
  • An uncaught exception occurred and the thread stopped unexpectedly

Both of these cases will cause the thread to terminate

Switch between threads

  • AFTER THE BLOCKED THREAD ENTERS THE RUNNABLE THREAD TO OBTAIN THE LOCK, IT CAN ENTER THE RUNNABLE STATE FROM THE BLOCKED, AND THE BLOCKED HAS NO TIMEOUT MECHANISM, IF THE LOCK IS NOT OBTAINED, IT WILL ALWAYS BLOCK
  • WAITING/TIMED_WAITING THREADS THAT ENTER THE RUNNABLE WAITING ARE FIRST WOKEN UP BY NOTIFY/NOTIFYALl, enter the blocked state, acquire a lock, and re-enter the RUNNABLE state

The life cycle of a Spring Bean

Comparison of several life cycles in Java

overview

The main life cycle of a Spring Bean is the following four phases:

  1. Instantiation (allocation of space) 2. Pre/post processor
  2. Property assignment (get/set/dependency injection)
  3. Initialization (init method) 5. Pre-post processor
  4. Destroy method

Servlet lifecycle

The servlet life cycle is mainly divided into the following four stages:

  • Loading phase
  • Initialization
  • Request Handling
  • Destruction phase

Loading phase

When the server starts (on first access, if loadOnStartup is set to a value other than -1 in the annotation, it is set to load and create an instance at server startup), the servlet class is loaded and a corresponding servlet instance is created. This process is only performed once (lazy loading mechanism).

  • Advantage: Save memory overhead
  • Cons: The first user has a bad experience
  • Add the annotation to the startup parameter loadOnStartup When the container is loaded, it starts when it is loaded
java           

Copy the code

@WebServlet(value = "/myServlet",loadOnStartup = 0)

Initialization

After the servlet instance is created, the container calls its init() method to complete the initialization operation. During initialization, the servlet can read configuration parameters and other contextual information and is ready to handle client requests.

Request Handling

After the servlet initialization is complete, the container forwards the client's request to the servlet instance and calls its service() method to process the request. The service() method will call the corresponding doGet(), doPost() and other methods according to the type of request (GET, POST, etc.) for processing. Can be accessed multiple times

Destruction phase

When the servlet instance is no longer needed, the container calls its destroy() method to release resources. This process will only be performed once. (You can also call the destroy method manually)

note

Phases in the servlet lifecycle are not set in stone. In some cases, such as when a servlet initialization fails or the container needs to reload the servlet class, the servlet lifecycle may be interrupted or restarted.