用于与Java运行时环境交互的类在Java中称为运行时类,它提供了Runtime.getRuntime()、exit(int status)、addShutdownHook(Thread hook)、Process exec( String命令)抛出输入输出异常,availableProcessors()、freeMemory()、totalMemory()等,用于进程执行、调用GC、获取总内存、获取空闲内存等,声明为public class Runtime 扩展了对象,并且只有一个 Java 应用程序只能使用 java.lang.Runtime 类的一个实例,并且使用 Java 中的 Runtime.getRuntime() 方法返回 Runtime 类的单例实例。
Java Runtime 类的方法
Java运行时类有多种方法。他们是:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
1.运行时 getRuntime()
使用 Runtime getRuntime() 方法返回 Run time 类的实例。考虑下面的 Java 程序来演示 Java 运行时类的 Runtime getRuntime() 方法。
代码:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { // The current runtime in association with process is obtained using getRuntime method. Runtime roll = Runtime.getRuntime(); // The current free memory for the current runtime is obtained using freeMemory() method. System.out.println("The current free memory for the current runtime is" + roll.freeMemory()); } }
输出:
说明:在上面的程序中,定义了一个名为program的类。然后调用main方法。然后使用 getRuntime 方法获取与进程关联的当前运行时间。然后使用 freeMemory() 方法获取当前运行时的当前可用内存。
2.退出(int状态)
使用 Runtime exit(int status) 方法终止当前虚拟机。考虑下面的 Java 程序来演示 Java 运行时类的 exit(int status) 方法。
代码:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //the current runtime or program is caused to exit using exit(int status) method Runtime.getRuntime().exit(0); //This statement is not executed because the program is terminated by the usage of exit(int status) method above System.out.println("Checking if the program executes this statement depsite the use of exit(int status) method"); } }
输出:
上述程序的输出是空白的。
说明:在上面的程序中,定义了一个名为program的类。然后调用main方法。然后使用 exit(int status) 方法导致当前运行时或程序退出。那么该语句就不会被执行,因为程序被上面语句中的 exit(int status) 方法终止了。
3. addShutdownHook(线程钩子)
使用addShutdownHook(Thread Hook)方法注册一个新的钩子线程。考虑下面的 Java 程序来演示 Java 运行时类的 addShutdownHook(Thread Hook) 方法。
代码:
//a class called program is defined public class program { // when the program is about to exit, this class extending the thread is called static class Mess extends Thread { public void run() { System.out.println("The program is going to exit"); } } //main method is called public static void main(String[] args) { try { //the class mess is registered as shut down hook Runtime.getRuntime().addShutdownHook(new Mess()); //The thread is made to sleep for certain seconds System.out.println("Five seconds of waiting time for the program to exit"); Thread.sleep(5); } catch (Exception ex) { ex.printStackTrace(); } } }
输出:
说明:在上面的程序中,定义了一个名为program的类。当程序即将退出时,会调用这个扩展线程的类。然后调用main方法。然后类混乱被注册为关闭钩子。然后线程会休眠几秒钟。
4.进程 exec(String command) 抛出 IOException
给定的命令是使用 Process exec(String command) throws IOException 方法在单独的进程中执行的。考虑下面的 Java 程序来演示 Process exec(String command) throws IOExceptionmethod of Java Run time class
代码:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { try { // a process is created to execute google chrome Process proc = Runtime.getRuntime().exec("google-chrome"); System.out.println("Successfully started google chrome"); } catch (Exception e) { e.printStackTrace(); } } }
输出:
说明:在上面的程序中,定义了一个名为program的类。然后调用main方法。然后创建一个进程来执行google chrome。
5.可用处理器()
可用处理器的数量使用 availableProcesors() 方法返回。考虑下面的 Java 程序来演示 Java 运行时类的 availableProcessors() 方法。
代码:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //checking for the number of available processors using availableprocessors() method. System.out.println("The number of available processors are " + Runtime.getRuntime().availableProcessors()); } }
输出:
说明:在上面的程序中,定义了一个名为program的类。然后调用main方法。然后使用 availableprocessors() 方法检查可用处理器的数量。
6.释放内存()
使用 freeMemory() 方法在 JVM 中返回可用内存量。
考虑下面的 Java 程序来演示 Java 运行时类的 freeMemory() 方法。
代码:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //The amount of free memory available is given by using freememory() method System.out.println("The amount of free memory available is " + Runtime.getRuntime().freeMemory()); } }
输出:
Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of free memory available is given by using freememory() method.
7. totalMemory()
The amount of total memory is returned in JVM using totalMemory()method. Consider the below Java program to demonstrate totalMemory() method of the Java Run time class.
Code:
//a class called program is defined public class program { //main method is called public static void main(String[] args) { //The amount of total memory available is given by using totalmemory() method System.out.println("The amount of free memory available is " + Runtime.getRuntime().totalMemory()); } }
Output:
Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of total memory available is given by using totalmemory() method.
以上是Java运行时类的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。

javaisnotirelyPlatemententduetojvmvariationsandnativecodinteintration,butitlargelyupholdsitsitsworapromise.1)javacompilestobytecoderunbythejvm

thejavavirtualmachine(JVM)IsanabtractComputingmachinecrucialforjavaexecutionasitrunsjavabytecode,使“ writeononce,runanywhere”能力

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多态性、Lambda表达式、StreamsAPI、泛型和异常处理。1.多态性让不同类的对象可以作为共同基类的对象使用。2.Lambda表达式使代码更简洁,特别适合处理集合和流。3.StreamsAPI高效处理大数据集,支持声明式操作。4.泛型提供类型安全和重用性,编译时捕获类型错误。5.异常处理帮助优雅处理错误,编写可靠软件。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3汉化版
中文版,非常好用

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具