JAVA API (ii) System class and runtime class

Source: Internet
Author: User
Tags time in milliseconds

1.System class and runtime class 1.1System class

The system class is not unfamiliar to us, and in the previously learned knowledge, when we need to print the results, we use the "System.out.println ()" Statement to print out the code, which uses the system class. This class defines system-related properties and methods that provide properties and methods that are static, so that you can refer to these properties and methods directly using the system class call. The following table is some of the methods commonly used by the system class.

Method declaration Function description
static void exit (int status) The method is used to terminate the currently running Java Virtual machine, where the parameter status indicates the state, and if the status code is not 0, it indicates an abnormal termination
static void GC () Run the garbage collector and recycle the garbage
Static native long Currenttimemillis (); Returns the current time in milliseconds
static void Arraycopy (Object src,int srcpos,object dest,int destpos,int length) Assigns a value from the specified source array referenced by SRC to an array of dest references, the assignment starts at the specified position and ends at the specified position in the destination array, overwriting the element in the corresponding position in the target array
Static Properties getProperties () Gets the current system properties
static string GetProperty (String key) Gets the system properties for the specified key description

    • GetProperties () method

The GetProperties () method is used to get all the properties of the current system, which returns a Properties object that encapsulates all of the system's properties, which exist as key-value pairs.

Example01.java

 Public classEXAMPLE01 { Public Static voidMain (string[] args) {//get the properties of the current systemProperties Properties =system.getproperties ();        System.out.println (properties); //Gets the key (property name) of all system properties, returning the Set objectSet<string> PropertyNames =Properties.stringpropertynames ();  for(String key:propertynames) {//Gets the current key (key) corresponding to the value (property value)String value =System.getproperty (key); SYSTEM.OUT.PRINTLN (Key+ "---->" +value); }    }}

The results of the operation are as follows:

    • Currenttimemillis () method

The method returns a Long that represents the difference between the current time and January 1, 1970 0 o'clock 0:0 0 seconds, in milliseconds, which is often referred to as a timestamp.

Example02.java

 Public classEXAMPLE02 { Public Static voidMain (string[] args) {LongStartTime = System.currenttimemillis ();//current time at which the loop starts        intsum = 0;  for(inti = 0; i < 100000000; i++) {sum+=i; }        LongEndTime = System.currenttimemillis ();//current time After the loop endsSYSTEM.OUT.PRINTLN ("program Run Time:" + (Endtime-starttime) + "MS"); }}

The results of the operation are as follows:

    • Arraycopy (Object src,int srcpos,object dest,int destpos,int length)

The Arraycopy () method is used to quickly copy elements from an array to another array, where the specific function of the parameter is as follows:

SRC: Represents the source array.

Dest: Represents the target array.

Srcpost: Represents the starting position of the copied element in the source array.

Destpos: Represents the starting position of the copy to the destination array.

Length: Indicates the number of copied elements.

  It is important to note that in the case of array copying, there must be enough copies of the elements in the source array to exist, and the target array must have enough space to hold the copied elements, otherwise there will be a cross-border exception.

Example03.java

 Public classExample03 { Public Static voidMain (string[] args) {int[] FromArray = {101,102,103,104,105,106};//Source Array        int[] ToArray = {201,202,203,204,205,206,207};//Target ArraySystem.arraycopy (FromArray, 2, ToArray, 3, 4); //print elements in the destination array         for(inti = 0; i < toarray.length; i++) {System.out.println (i+ ":" +Toarray[i]); }    }}

The results of the operation are as follows:

In addition to the methods in the example above, the system class has two common methods, namely the GC () and exit (int status) methods. The GC () method is used to start the Java garbage collector and to reclaim the garbage objects in memory. The exit (int status) method is used to terminate the currently running Java Virtual machine, where the parameter status is used to represent the current exception state, usually specified as 0, which indicates a graceful exit, or an abnormal termination.

1.2Runtime class

The runtime class represents the state of the virtual machine runtime and is used to encapsulate the VM process. Each time you start a virtual machine with a Java command that corresponds to a runtime instance, and there is only one instance, the class is designed with a singleton pattern, and objects cannot be instantiated directly. To get a runtime instance in a program, you can only use the following methods:

Runtime run = Runtime.getruntime ();

Because the virtual machine process is encapsulated in a runtime instance, it is common in the program to obtain information about the current virtual machine through the instance object of the class.

Example04.java

 Public class Example04 {    publicstaticvoid  main (string[] args) {        = Runtime.getruntime (); // get runtime instance        System.out.println ("Number of processors:" + rt.availableprocessors () + "x");        System.out.println ("Amount of free Memory:" + rt.freememory ()/1024/1024 + "M");        System.out.println ("Maximum available memory:" + rt.maxmemory ()/1024/1024 + "M");}    }

The results of the operation are as follows:

In the above example, the Availableprocessors () method, the Freememory () method, and the MaxMemory () method calculate the number of processors in the current virtual machine, the number of free memory and the maximum amount of memory, in bytes.

The runtime class provides an Exec () method used to execute a DOS command that achieves the same effect as entering a DOS command in a command-line window, as in the following example:

Example05.java

 Public class Example05 {    publicstaticvoidthrows  IOException {        = Runtime.getruntime (); // To create a runtime instance object        Rt.exec ("notepad.exe"); // call the Exec () method     }}

The exec () method of the runtime class returns a Process object that represents one of the operating system's processes through which the resulting new process can be managed, such as requiring the shutdown process to call the Destroy () method only.

Example06.java

 Public class Example06 {    publicstaticvoidthrows  IOException, interruptedexception {        = Runtime.getruntime ();   Create runtime instance object        Process process = Rt.exec ("notepad.exe");   Call the Exec () method        thread.sleep ();   program sleeps 3 seconds        Process.destroy ();   Kill Process     }}

JAVA API (ii) System class and runtime class

Related Article

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.