Java notes 13. Common Api-runtime, runable, System classes

Source: Internet
Author: User
Tags terminates

Common Api-runtime, runable, System classes

First, the runtime class

1. Overview: Runtime classencapsulates the running process of the Java command itselfThat every Java application has a runtime class object that allows it to interact with the running environment while the application is running, but it is important to note that the application itself cannotCreateruntime Class object. Many methods of the runtime class and the methods in the system want to repeat, wecan'tcreate a runtime instance directly, but you canRuntime.getruntime by static methodGetis runningof thereferences to runtime objects. Exec Method: After the Java command is run, it is itself a process on a multi-tasking operating system, wherestart a new process in a process, which is to use the Exce method when executing other programs. The Exec method returns a process class object that represents the child process, throughwhich theJava process can interact with the child process . 2.API Introduction(1) Succession relationsJava.lang.Object <---- Java.lang.Runtime (2) Return runtime object for the current applicationruntime runtime=runtime. GetRuntime ();(3) Important methods process EXEC: Executes the specified string command in a separate process process exec (string[] cmdarray, string[] envp): Executes specified commands and parameters in a separate process based on the specified environment void GC (): Start the garbage collection bin Static Runtime GetRuntime () : Returns the Runtime object for the current Java application void Halt (int status) : Mandatory termination of the currently running Java Virtual machineMethods inherited from class Java.lang.Objectclone, Equals, Finalize, GetClass, Hashcode, notify, Notifyall, toString, wait, wait, wait 3.Runtime Application Development(1) Start a new child process in the current processFirst, we get the Runtime class object of the current application through the runtime.getruntime () method , and then call the Exce method through the object to start a new child process and implements the interaction of the Java program with the subprocess through the returned process object. Runtime run=runtime.getruntime (); Returns the runtime object for the current applicationProcess p=run.exec ("notepad.exe");//Start a new process in the current process (open Notepad)(2) example source code
<span style= "FONT-SIZE:14PX;" >/* implementation: Start a child process after running the application: Open the Runtimetest.java with Windows Notepad * and destroy the child process after 5 seconds, the Notepad program is turned off */public class Runtimetest { public static void Main (string[] args) {  Process p=null;  try{   Runtime Run=runtime.getruntime ()//returns the runtime object of the current application   p=run.exec ("notepad.exe");//Start a new process in the current process   Thread.Sleep ();  }  catch (Exception e)  {   System.out.println (e.getmessage ());  }  P.destroy (); }}</span>


Second, runnable interface
1. Overview : The Runnable interface is primarily used to provide a common protocol for executing specified code in an object that is active, for example, the Runable interface can be inherited by thread, The so-called active state means that the thread has been developed and is not finished yet. The runnable interface can be inherited by any class, and instances of these classes will be executed in the thread and a parameterless run method needs to be implemented in these classes without inheriting the thread.
2. Important Methods : in the thread of the individual designation
void Run ()
    When an object inherits the Runnable interface is used to create a thread, Starting the thread will call the object's Run method
third, System interface
1. Overview: Java does not support global functions and variables , and the Java designer collects some important system-related methods and variables into a class of consent, which is the Systemt class. The system class cannot be instantiated , all members of the Syetem class are static, and when we want to reference these variables and methods, we use the System class title prefix directly. The main functions of the SYSTEMT class are: standard input stream, standard output stream, standard error output stream, environment variable, loading files and libraries;quickly copy part of an array, and so on. 2. Important Methods: static PrintStream err : standard error output stream static InputStream in: standard input stream Static PrintStream out: standard output stream static void exit (int status): Terminates the operation of the virtual machine prematurely. For an exception, the item terminates the operation of the virtual machine, passing a non-0 value as the parameter. For the normal operation of the user, terminate the operation of the virtual machine, passing 0 values as parameters; Static Long Currenttimemillis ():returns the time, in milliseconds, since January 1, 1970 0:0 0 seconds, which is a long type of large value.
static Properties getProperties ():gets the environment properties for the current virtual machine. Windows-like environment properties, where path and classpath are two environment variables, each of which is a variable with a value in the form of a pair. Similarly, Java as a virtual operating system, it also has its own environment properties,Propertiesis a subclass of Hashtable that can be used to store data in multiple variable/value pairs in environment properties. The GetProperties method return value is an object that contains the properties type of all environment attributes for the current virtual machine . static string SetProperty (string key, String value): Sets the new environment variable
static void load (String filename): Loads a local file 3.System Application Development Tips(1) Calculate the time it takes for a program code to runLong Starttime=system.currenttimemillis ();...//code snippetLong Endtime=system.currenttimemillis ();//Where Long is a data type class, endTime is an object rather than a long integer variable System.out.println ("Total time expended is" + (Starttime-endtime) + "milliseconds");(2) example source code
<span style= "FONT-SIZE:14PX;" >import Java.util.enumeration;import java.util.properties;/* implementation: Get all environment variables in the Java Virtual machine */public class SystemInfo {public static void Main (string[] args) {  properties p=system.getproperties ();//Returns a Properties instance object  enumeration e= P.propertynames (); Returns the properties of all the key objects to the enumeration object while  (E.hasmoreelements ())//checks whether all elements in the enumeration object have traversed  {   String key= (String) e.nextelement (); Iterate through each object in the property list   System.out.println (key+ "=" +p.getproperty (key))///print the value of the key object of an element in the attribute list and its corresponding value of the object  }}}</ Span>


The effect is as follows:
Reference:http://docs.oracle.com/javase/8/docs/api/index.html

Java notes 13. Common Api-runtime, runable, System classes

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.