import cn.hutool.core.util.RuntimeUtil;import com.sun.xml.internal.bind.v2.runtime.reflect.opt.FieldAccessor_Ref;import org.junit.Test;import org.junit.rules.RunRules;import java.io.File;import java.io.IOException;import java.lang.reflect.Proxy;/*** @author Summerday*/public class RuntimeTest {@Testpublic void testExec(){Process process = null;try {// 打开记事本process = Runtime.getRuntime().exec("notepad");Thread.sleep(2000);} catch (IOException | InterruptedException e) {e.printStackTrace();}finally {if(process != null){process.destroy();}}}@Testpublic void testAvailableProcessors(){System.out.println(Runtime.getRuntime().availableProcessors());}@Testpublic void testFreeMemory(){Runtime r = Runtime.getRuntime();System.out.println("处理器个数: " + r.availableProcessors());System.out.println("最大内存 : " + r.maxMemory());System.out.println("总内存 : " + r.totalMemory());System.out.println("剩余内存: " + r.freeMemory());System.out.println("最大可用内存: " + getUsableMemory());for(int i = 0; i < 10000; i ++){new Object();}System.out.println("创建10000个实例之后, 剩余内存: " + r.freeMemory());r.gc();System.out.println("gc之后, 剩余内存: " + r.freeMemory());}/*** 获得JVM最大可用内存 = 最大内存-总内存+剩余内存*/private long getUsableMemory() {Runtime r = Runtime.getRuntime();return r.maxMemory() - r.totalMemory() + r.freeMemory();}@Testpublic void testAddShutdownHook() throws InterruptedException {Runtime.getRuntime().addShutdownHook(new Thread(()-> System.out.println("programming exit!")));System.out.println("sleep 3s");Thread.sleep(3000);System.out.println("over");}@Testpublic void testRemoveShutdownHook() throws InterruptedException {Thread thread = new Thread(()-> System.out.println("programming exit!"));Runtime.getRuntime().addShutdownHook(thread);System.out.println("sleep 3s");Thread.sleep(3000);Runtime.getRuntime().removeShutdownHook(thread);System.out.println("over");}@Testpublic void testExit(){Runtime.getRuntime().exit(0);//下面这段 无事发生System.out.println("Program Running Check");}@Testpublic void testHalt(){Runtime r = Runtime.getRuntime();r.halt(0);System.out.println("process is still running");}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。