Java Process詳解及實(shí)例
Runtime
Java可以通過Runtime來調(diào)用其他進(jìn)程,如cmd命令,shell文件的執(zhí)行等??梢詰?yīng)該該類設(shè)置系統(tǒng)時(shí)間,執(zhí)行shell文件。此處記錄幾個(gè)有用應(yīng)用如下。
設(shè)置本地時(shí)間
可以調(diào)用cmd /c date命令,完成本地時(shí)間設(shè)置,不過這個(gè)命令在win7下可以使用,但是win10需要管理員權(quán)限,可能無法設(shè)置系統(tǒng)時(shí)間。win7下使用Java實(shí)現(xiàn)修改本地時(shí)間代碼如下,需要注意的是waitFor是必須的,否則無法立即生效。
/** * 設(shè)置本地日期 * @param date yyyy-MM-dd格式 */ private static void setSystemDate(String date){ Process process = null; String command1 = "cmd /c date "+date; System.out.println(command1); try { process = Runtime.getRuntime().exec(command1); //必須等待該進(jìn)程結(jié)束,否則時(shí)間設(shè)置就無法生效 process.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); }finally{ if(process!=null){ process.destroy(); } } }
網(wǎng)卡吞吐量計(jì)算
可以通過cat /proc/net/dev命令獲取網(wǎng)卡信息,兩次獲取網(wǎng)卡發(fā)送和接收數(shù)據(jù)包的信息,來計(jì)算網(wǎng)卡吞吐量。實(shí)現(xiàn)如下:
/** * @Purpose:采集網(wǎng)絡(luò)帶寬使用量 * @param args * @return float,網(wǎng)絡(luò)帶寬已使用量 */ public static Double getNetworkThoughput() { Double curRate = 0.0; Runtime r = Runtime.getRuntime(); // 第一次采集流量數(shù)據(jù) long startTime = System.currentTimeMillis(); long total1 = calculateThoughout(r); // 休眠1秒后,再次收集 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // 第二次采集流量數(shù)據(jù) long endTime = System.currentTimeMillis(); long total2 = calculateThoughout(r); // 計(jì)算該段時(shí)間內(nèi)的吞吐量:單位為Mbps(million bit per second) double interval = (endTime-startTime)/1000; curRate = (total2-total1)*8/1000000*interval; System.out.println("收集網(wǎng)絡(luò)帶寬使用率結(jié)束,當(dāng)前設(shè)備的網(wǎng)卡吞吐量為:"+(curRate)+"Mbps."); return curRate; } /** * 計(jì)算某個(gè)時(shí)刻網(wǎng)卡的收發(fā)數(shù)據(jù)總量 * @param runtime * @return */ private static long calculateThoughout(Runtime runtime){ Process process = null; String command = "cat /proc/net/dev"; BufferedReader reader = null; String line = null; long total = 0; try { process = runtime.exec(command); reader = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = reader.readLine()) != null) { line = line.trim(); // 考慮多網(wǎng)卡的情況 if (line.startsWith("eth")) { log.debug(line); line = line.substring(5).trim(); String[] temp = line.split("\\s+"); total+=(Long.parseLong(temp[0].trim()));// Receive total+=(Long.parseLong(temp[8].trim()));// Transmit } } } catch (NumberFormatException | IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (process != null) { process.destroy(); } } return total; }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java Annotation注解相關(guān)原理代碼總結(jié)
這篇文章主要介紹了Java Annotation注解相關(guān)原理代碼總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07@Slf4j?如何實(shí)現(xiàn)日志輸入到外部文件
這篇文章主要介紹了@Slf4j?如何實(shí)現(xiàn)日志輸入到外部文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12詳解Spring系列之@ComponentScan批量注冊(cè)bean
本文介紹各種@ComponentScan批量掃描注冊(cè)bean的基本使用以及進(jìn)階用法和@Componet及其衍生注解使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-02-02淺談web服務(wù)器項(xiàng)目中靜態(tài)請(qǐng)求和動(dòng)態(tài)請(qǐng)求處理
這篇文章主要介紹了淺談web服務(wù)器項(xiàng)目中靜態(tài)請(qǐng)求和動(dòng)態(tài)請(qǐng)求處理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07