[Art reading notes for concurrent programming] Understanding how java executes multithreading from the memory map

Understand how java executes multithreading from memory graph

1. Introduction to memory graph

As we all know, the member variables in the java class will be saved in the method area, and the java runtime method will be stored in the stack. Then the local variables in the method are also stored in the stack, and the reference type (new object) is stored in the heap memory. The following uses the method area, stack memory, and heap memory in the java memory to demonstrate the execution process of the java method.

insert image description here

First define a Person class.

public class Person {
    
    
    public int age;
    public String name;
    
	public void m1() {
    
    
	}
	public void m2() {
    
    
		m3();
	}
	public void m3() {
    
    
	}
	
	public static void m4() {
    
    
	}
}

At this point our memory map looks like this

insert image description here

Define another test class

public class Test {
    
    
	public static void main(String[] args) throws Exception{
    
    
		Person person1 = new Person();
		person1.m1();
	}
}

At this point our memory map looks like this

insert image description here

2. Single-threaded code execution mode

After starting the Test class, create a main thread stack and create a person1 object (stored in heap memory and only carry non-static members in the class)
insert image description here

Then the person1 object calls the m1 method, and the main thread stack copies a copy of the m1 method from the address of the person1 object in the heap memory and pushes it onto the stack

insert image description here

After m1 is executed, the m1 method is popped out of the stack, the content of the entire main thread ends, the methods are popped out of the stack one by one, and the created object references are destroyed.

insert image description here

The person1 object in the heap memory will be recycled if it is not referenced elsewhere.

insert image description here

The main method is executed

3. Multi-threaded code execution mode

Adjust the Test class to allow multiple threads to participate

public class Test {
    
    
	public static void main(String[] args) throws Exception{
    
    
		Thread thread1 = new Thread() {
    
    
			@Override
			public void run() {
    
    
				Person person1 = new Person();
				person1.m2();
			}
		};
		Thread thread2 = new Thread() {
    
    
			@Override
			public void run() {
    
    
				Person person1 = new Person();
				person1.m3();
			}
		};
		// -----------------------

		thread1.start();
		thread2.start();
		
		Person person1 = new Person();
		person1.m1();
	}
}

The memory map when executing to the dividing line is as follows

insert image description here

Then execute the start method to throw the thread into the ready queue

insert image description here

Since threads 1 and 2 both enter the ready state. The cpu may allocate time slices at any time to make it run, so the order of creating person1 in thread1 and thread2 and creating person1 objects in the main thread cannot be determined. It may be that the object of person1 in the main thread has been executed after the method is recycled, thread1 and thread 2 start to create, or three person1 objects may exist at the same time.

insert image description here

Guess you like

Origin blog.csdn.net/qq_51383106/article/details/131678878