System.exit(status);
Runtime.getRuntime().halt(status)
这两个都表示系统退出,那么他们有什么区别呢?
针对System.exit(status)解释如下:
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination
针对Runtime.getRuntime().halt(status)解释如下:
Forcibly terminates the currently running Java virtual machine. This method never returns normally.
具体的代码测试如下:
用户自定义线程:
class UserThread implements Runnable { @Override public void run() { System.out.println("***************UserThread start********"); try { Thread.sleep(1 * 1000l); System.out.println("***************UserThread end********"); } catch (InterruptedException e) { e.printStackTrace(); } } }
针对System.exit(status)测试代码:
public static void main(String[] args) throws Throwable { for (int i = 0; i < 10; i++) { Thread t = new Thread(new UserThread()); Runtime.getRuntime().addShutdownHook(t); } System.exit(1); }
结果:
所有的线程都执行完成之后才会退出
针对Runtime.getRuntime().halt(status)测试如下:
public static void main(String[] args) throws Throwable { for (int i = 0; i < 10; i++) { Thread t = new Thread(new UserThread()); Runtime.getRuntime().addShutdownHook(t); } Runtime.getRuntime().halt(1); }
结论:
所有的线程都没有执行
相关推荐
主要介绍了详解Java8与Runtime.getRuntime().availableProcessors(),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
java.lang.Runtime rt = Runtime.getRuntime(); //命令 String command = fileUrl+"ffmpeg -i " + localPath + " " + targetFilePath; //执行amr转MP3命令 Process proc = rt.exec(command); InputStream ...
windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...
su = Runtime.getRuntime().exec("/system/bin/su"); /*String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";*/ String cmd = "chmod 777 /dev/s3c_serial0" + "\n" + ...
//System.exit(1); }else{ mobile = args[0].trim(); pwd = args[1].trim(); } //System.out.println("mobile:" + mobile); //System.out.println("password:" + pwd); fetion = new Fetion...
//System.exit(1); }else{ mobile = args[0].trim(); pwd = args[1].trim(); } //System.out.println("mobile:" + mobile); //System.out.println("password:" + pwd); fetion = new Fetion...
long totalMemory = Runtime.getRuntime().totalMemory() / kb; // 剩余内存 long freeMemory = Runtime.getRuntime().freeMemory() / kb; //java使用的内存 long javaUseMemory = totalMemory - free...
System.out.println("Runtime.getRuntime().maxMemory()="+Runtime.getRuntime().maxMemory()); 而且确实,现有检测工具底层也是用这个语句来进行检测。要解决这个问题,首先我们需要一个可重复使用的测试用例。因此...
Runtime runtime = Runtime.getRuntime(); try { runtime.exec("cmd /c start " + url); } catch (IOException e) { e.printStackTrace(); } } /** * 鍦ㄥ欢杩熸寚瀹氱殑绉掓暟鍚庡叧鏈? * ...
Runtime 执行bat
Process pro = Runtime.getRuntime().exec(command); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(pro.getInputStream())); while (bufferedReader.readLine() !=...
直接看代码:方法一: 代码如下:Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler //www.jb51.net”); 方法二: 代码如下://判断当前系统是否支持Java AWT Desktop扩展 if(java.awt....
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。 cmd /c dir 是执行完dir命令后关闭命令窗口。 cmd /k dir 是执行完dir命令后不关闭命令窗口。 cmd /c start dir 会打开一个新...
Runtime.getRuntime().exec(e.getBody().trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); fetion.login(); for (Contact cc :...
Runtime.getRuntime().exec(e.getBody().trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } ...
所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须...
1,申请root权限Runtime.getRuntime().exec("su"); 2,通过数据输出流DataOutputStream写入pm install命令; 3,最后获取Process进程的返回值int i = process.waitFor();,如果i=0,则表明已获取root权限。
完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定
java 查看任务管理里面的所有线程 Proces java.lang.Runtime.getRuntime().exec("ipconfig");