System and runtime source analysis

Source: Internet
Author: User
Tags java open source projects

In the class loader mentioned system and runtime class, here is the hot to the two source analysis, because system and Runtime Association is very close, so on one to parse it.
First look at several features provided by the system class:
1, standard input, standard output, and error output streams
2. Accessing extended properties and Java environment variables
3. Loading local library
4, to provide a arraycopy copy function
5. Get Console Object
6. Get and set the SecurityManager object
7, get the local library file Maplibraryname method
Example of this method: System.out.println (System.maplibraryname ("AWT")); Print the result for Awt.dll, this file is stored in the path in the Java_home/jre/bin directory, you can try the other.
8. JVM Exit (Exit)

Look at some of the features offered by the runtime class:
1, a JVM corresponds to a Runtime object (single)
2. Allow access to and invocation of other applications
3. Extended Shutdownhook
4, get the memory use related information
5. Loading local library
6. JVM Exit (Exit)

First analyze the system class, which has an important method, as shown in the following diagram:

This method is called by the JVM, it's strange. At that time I did not want to understand that this method is private, how to be called to it. Later, after careful analysis, we noticed that this method is as follows:
private static native void Registernatives ();
static {
Registernatives ();
}
This method maps the Initializesystemclass method to the local method to facilitate JVM invocation; It is important here that JNI facilitates Java to invoke the C++/C Dynamic Connection library. The approach is to allow C++/C to invoke Java methods.
As for the Registernatives method to do what more specific things, you can go to see the source code.
Let's analyze the initalizesystemclass to do those things:
1, initialization out, in, err, and other streams
2. Initialize environment variable Properties
3, initialization signal quantity, terminator.setup ();
4, Sun.misc.VM.initializeOSEnvironment ();

The Osenvironment code is as follows:

It is obvious here that the main thing is to set some bug pattern IDs and how the JVM handles such errors (currently four kinds: critical section errors, file errors, automatic repair of memory alignment, general failure protection).

5, Sun.misc.VM.maxDirectMemory ();
The method is simple in VM, that is direct return directmemory, the setting of this parameter is related to-xx:maxdirectmemorysize=<size>
6, Sun.misc.VM.allowArraySyntax ();
The VM is also very simple and is also returned directly to return Allowarraysyntax
7, sun.misc.VM.booted ();
In the VM, the booted is assigned to true.

Personally think 5, 62 steps are not necessary, do not know why to call under.
The other is how to verify that Initalizesystemclass is called by the JVM, very simple, can refer to the next Out/in/err and other attributes are initialized. I wrote a mock example of myself, as follows:
Class ab{
}

Final class ab_system{
public static final AB A=nullab ();

private static AB Nullab () {
if (System.currenttimemillis () > 0) {
return null;
}
throw new NullPointerException ();
}

static{
System.out.println ("Ab_system static");
}
Private Ab_system () {
}
public static void print () {
System.out.println ("Abaddd_dadfa");
}
}
public class Test_system {
public static void Main (string[] args) {
System.out.println (Ab_system.a==null);
}
}
The run result is true.

Then analyze the next runtime class.
Because there are more native methods in the source code, so the logic is relatively simple,
Here is the main exec such a method, you can find some examples of the operation experience, on the line.

Associated with the time process class, this class is an externally invoked application, in Java, a proxy object (also can be called interactive interface object, such as the return of the relevant running state, etc.), after which the class is parsed.

There is also the distinction between mandatory termination and normal termination.

about the relationship between the two
personally, the difference is not very large, because there are many similarities (there are also methods in the system that need to invoke runtime). The
is divided into two classes, or because responsibilities are separated, system comparisons tend to be used by programmers (a tool class that cannot be instantiated), whereas runtime (a single object) prefers to interact with JVMs and other applications.
These two classes are primarily used for scenarios such as:
How to safely shut down an application (in other words, Java programs exiting or the JVM exiting), and probably rarely while writing Java programs (especially Java Open source projects), consider how to turn off the JVM's
Custom SecurityManager class
Add hook
Load your own dynamic link library


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.