JVM Java seemingly a language, in fact a huge system of the kingdom, developed so many years, still do not understand, I thought I understand, but after a period of time and forget, so said still didn't understand
1, the main method of speaking
After compiling our Java file, you need to have a class with the Main method, and the Java command will instruct the operating system to start a JVM process
After the JVM process starts, look for that main place to start executing the program
java [jvm_options] classname_with_main [Args_separate_space]
The signature of the Main method must be pubic static void main (string[] args) why?
Simple point:
First, the main method is that the JVM (Java Virtual machine) automatically calls
The location of the JVM's call to the main method is naturally not in a class, or in a package, so the JVM is visible only when the main method is on the public level, so the Mian method needs to be decorated.
The class that contains the main method also requires the public modifier.
Because the main method is the entry for all programs, that is, when main is called, no object is created, a method is called without an object, and only the method is defined as a static method, so the main method is a static method that requires static modification.
The JVM is already at the bottom of the Java program, and the return value of the method called by it has no place to go, so the main method return value is empty and requires void decoration.
As to the parameter of the main method string[] arg we have little chance to use it now, it is used to accept arguments passed in the command line
2, the main method of execution before the god Horse
You can see the JVM source code analysis
The first thing to make clear is that the JVM process is the operating system process, which is a multithreaded mechanism
We identify two types of threads:
JVM thread : Refers to a thread that the JVM manages itself, which we cannot manipulate in the program, mostly the Guardian type
java Threading : refers to the Java technology perspective of the JVM, we are in the program with the thread class or the Runnable interface to write generated threads, controllable threads
As to how Java threads are implemented in the JVM, and how they correspond to OS-level threads, see http://my.oschina.net/jingxing05/blog/275334
After you have identified two different types of threads, before you execute the Main method: LOADJAVAVM
The JVM process initiated multiple JVM threads ( most likely wrong, if any, please enlighten ):
JVM Threads:
-
Start VM thread, Singleton, all threads ancestor! This thread self-polls the loop from a queue to take the action task, to produce other threads
-
According to the JVM abstraction specification, there may be execution engine threads, GC threads, ClassLoader Threads
After the JVM itself starts and initializes, it
Continueinnewthread (Javamain, Threadstacksize, (void*) &args);
The main method that starts a thread called Main to execute the portal, although the main thread is not the thread that we manually produced, TA is still a non-daemon thread
3. Main execution process
When the main method is executed, the JVM process discovers that the main class is not in the method area and begins to Classload
The final step in the class loading is to decide whether to initialize the class as appropriate
Before main executes, the class must be initialized. Initializes the variables of the class, as well as static code blocks. The parent class of the initialization is initialized first. Each class has an implied parent class object.
Order of initialization: class variables and static blocks ordered, after the father's son
The initialization process of a class occurs at the moment:
1. T is a class, when an instance of T is created, that is t t = new t ();
2. When a static method of T is called, it is T.staticfield ();
3. When the static property of T is assigned, T.staticfield = O;
4. When a static property of T is used, that is, Object o = T.staticfield; But it is not a constant.
5. T is a top level class, and an Assert statement lexically nested
Within T is executed. (Do not understand, solve)
The method needs parameters, local variables, local methods, operands such as the structure of the stack frame push to the stack area of the main thread, then the execution engine thread begins execution, executes, and pops out the stack frame. Main thread fades when the stack area of the main thread does not have a stack frame.
This step is an optimized step to release some of the method area of memory, the JVM itself decided not to this step, generally do not unload the method area
1. All non-daemon threads are terminated.
2. A thread invokes the class runtime or the system exit method
Main program Execution diagram
Class load Verbose procedure
Some threads in the JVM can be see http://ifeve.com/jvm-thread/
HTTP://MY.OSCHINA.NET/JINGXING05/BLOG/282867?P={{CURRENTPAGE+1}}
The story behind the Java Main method? Go