By using java.lang.Runtime.addShutdownHook(Thread hook)
DemoRuntime.java
[java]
package com.SPlessons;
public class DemoRuntime {
// a class that extends thread that is to be called when program is exiting
static class Message extends Thread {
public void run() {
System.out.println("Bye-Bye.");
}
}
public static void main(String[] args) {
try {
// register Message as shutdown hook
Runtime.getRuntime().addShutdownHook(new Message());
// print the state of the program
System.out.println("Here Program is starting...");
// cause thread to sleep for 3 seconds
System.out.println("Here Waiting for 3 seconds...");
Thread.sleep(3000);
// print that the program is closing
System.out.println("Program is closing...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
[/java]
The run() strategy gives a passage indicate the string and developer will put the finish business rationale inside this technique. To return the singleton instance Runtime.getRuntime() method will be used.
[java] public void run()[/java]
Output: When compile the code result will be as follows.
[java]
Here Program is starting...
Here Waiting for 3 seconds...
Program is closing...
Bye-Bye.
[/java]
By using java.lang.Runtime.freeMemory() method
[java]
package com.SPlessons;
public class DemoRuntime {
public static void main(String[] args) {
// print when the program starts
System.out.println("Here Program is starting...");
// print the number of free bytes
System.out.println("" + Runtime.getRuntime().freeMemory());
}
}
[/java]
The functionality of java.lang.Runtime.freeMemory() method is to return free memory in JVM. Following is the syntax declaration.
[java]public long freeMemory()[/java]
Output: When compile the code result will be as follows.
[java]
Here Program is starting...
15796032
[/java]
By using java.lang.Runtime.gc() method
[java]package com.SPlessons;
public class DemoRuntime {
public static void main(String[] args) {
// print when the program starts
System.out.println("Here Program is starting...");
// run the garbage collector
System.out.println("Running Garbage Collector...");
Runtime.getRuntime().gc();
System.out.println("Completed.");
}
}
[/java]
The garbage collector is used to remove the unused objects in the code, to free up the space. Following is the syntax declaration of the method.
[java]public void gc()[/java]
Output: Now compile the code result will be as follows.
[java]
Here Program is starting...
Running Garbage Collector...
Completed.
[/java]
By using java.lang.Runtime.totalMemory() method
[java]
package com.SPlessons;
public class DemoRuntime {
public static void main(String[] args) {
// print the state of the program
System.out.println("Here Program is starting...");
// print the total memory
System.out.println("" + Runtime.getRuntime().totalMemory());
}
}
[/java]
Output: When compile the code result will be as follows.
[java]Here Program is starting...
16252928
[/java]
By using java.lang.Runtime.loadLibrary(String filename) method
[java]
package com.SPlessons;
public class DemoRuntime {
public static void main(String[] args) {
// print when the program starts
System.out.println("Here program is starting...");
// load a library that is Windows/System32 folder
System.out.println("Here loading the library...");
Runtime.getRuntime().loadLibrary("C:/Windows/System32/crypt32.dll");
System.out.println("Library Loaded.");
}
}
[/java]
This strategy stacks the dynamic library with the predetermined library name. A document containing local code is stacked from the neighborhood record framework from a place where library documents are ordinarily acquired. The points of interest of this procedure are usage subordinate. The mapping from a library name to a particular filename is done in a framework particular way. Following is the syntax declaration of java.lang.Runtime.loadLibrary(String filename) method.
[java]public void loadLibrary(String libname)[/java]
Output: When compile the code result will be as follows.
[java]
Here program is starting...
Here loading the library...
Library Loaded.[/java]