General usage of the System class and Runtime class

  • 2020-04-01 02:51:17
  • OfStack

Common usage of the System class
1. Mainly obtain the information of environment variables of the system


public static void sysProp()throws Exception{
  Map<String,String> env = System.getenv();
  //Gets all the environment variables for the system
  for(String name : env.keySet()){
   System.out.println(name + " : " +env.get(name));
  }
  //Gets the value of the specified environment variable for the system
  System.out.println(env.get("JAVA_HOME"));
  //Gets all the attributes of the system
  Properties prop = System.getProperties();
  //Save the system properties to a configuration file
  prop.store(new FileOutputStream("Prop.properties"),"System properties");
  //Outputs specific system properties
  System.out.println(System.getProperty("os.name"));
 }

2. Method operation related to system time


public static void sysTime(){
  //Gets the current time of the system in milliseconds currentTimeMillis() (returns the time difference between the current time and UTC 1970.1.1 00:00)
  Long time = System.currentTimeMillis();
  System.out.println(time);

  Long time1 = System.nanoTime();//It is mainly used to calculate the time difference in nanoseconds
  Long time3 = System.currentTimeMillis();
  for(Long i =0l ;i <999l; i++){}
  Long time2 = System.nanoTime();
  Long time4 = System.currentTimeMillis();
  System.out.println(time2 - time1+ " : " +(time4 - time3));
 }

3. Verify that two objects are the same in heap memory


public static void identityHashCode(){
  //Str1 and str2 are two different String objects
  String str1 = new String("helloWorld");
  String str2 = new String("helloWorld");
  //Since the String class overrides the hashCode() method, their hashCode is the same
  System.out.println(str1.hashCode()+" : "+str2.hashCode());
  //Because they're not the same object their calculated HashCode is different.
  //In fact, the most primitive HashCode calculation method used by this method is the HashCode calculation method of Object
  System.out.println(System.identityHashCode(str1) + " : "+ System.identityHashCode(str2));
  String str3 = "hello";
  String str4 = "hello";
  //Since they refer to the same object in the constant pool, their HashCode is the same
  System.out.println(System.identityHashCode(str3) + " : "+ System.identityHashCode(str4));
  
 }

A common use of the Runtime class
Each Java application has an instance of the Runtime class that enables the application to connect to the environment in which it is running.


class RunTimeTest 
{
 public static void main(String[] args) throws Exception
 {
  getJvmInfo();
  //execTest();
 }
 public static void getJvmInfo(){
  //Gets the runtime objects associated with the Java runtime
  Runtime rt = Runtime.getRuntime();
  System.out.println(" Number of processors: " + rt.availableProcessors()+" byte");
  System.out.println("Jvm The total number of memory   : "+ rt.totalMemory()+" byte");
  System.out.println("Jvm Free memory:  "+ rt.freeMemory()+" byte");
  System.out.println("Jvm Maximum memory available:  "+ rt.maxMemory()+" byte");
 }
 public static void execTest()throws Exception{
  Runtime rt = Runtime.getRuntime();
  //Executes the specified string command in a separate process.
  rt.exec("mspaint E:\mmm.jpg");
 }
}


Related articles: