温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java Process详解及实例

发布时间:2020-08-23 17:12:14 来源:脚本之家 阅读:248 作者:lqh 栏目:编程语言

Runtime

Java可以通过Runtime来调用其他进程,如cmd命令,shell文件的执行等。可以应该该类设置系统时间,执行shell文件。此处记录几个有用应用如下。

设置本地时间

可以调用cmd /c date命令,完成本地时间设置,不过这个命令在win7下可以使用,但是win10需要管理员权限,可能无法设置系统时间。win7下使用Java实现修改本地时间代码如下,需要注意的是waitFor是必须的,否则无法立即生效。

 /**
   * 设置本地日期
   * @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);
      //必须等待该进程结束,否则时间设置就无法生效
      process.waitFor();
    } catch (IOException | InterruptedException e) {
      e.printStackTrace();
    }finally{
      if(process!=null){
        process.destroy();
      }
    }
  }

网卡吞吐量计算

可以通过cat /proc/net/dev命令获取网卡信息,两次获取网卡发送和接收数据包的信息,来计算网卡吞吐量。实现如下:

 /**
   * @Purpose:采集网络带宽使用量
   * @param args
   * @return float,网络带宽已使用量
   */
  public static Double getNetworkThoughput() {
     Double curRate = 0.0;
    Runtime r = Runtime.getRuntime();

    // 第一次采集流量数据
    long startTime = System.currentTimeMillis();
    long total1 = calculateThoughout(r);

    // 休眠1秒后,再次收集
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // 第二次采集流量数据
    long endTime = System.currentTimeMillis();
    long total2 = calculateThoughout(r);

    // 计算该段时间内的吞吐量:单位为Mbps(million bit per second)
    double interval = (endTime-startTime)/1000;
    curRate = (total2-total1)*8/1000000*interval;

    System.out.println("收集网络带宽使用率结束,当前设备的网卡吞吐量为:"+(curRate)+"Mbps.");
    return curRate;
  }

  /**
   * 计算某个时刻网卡的收发数据总量
   * @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();
        // 考虑多网卡的情况
        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;
  }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI