当前位置: 网学 > 编程文档 > JAVA > 正文

用java取得linux系统cpu、内存的实时信息

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/16
下载{$ArticleTitle}原创论文样式

   * @throws InterruptedException
   */
   public static float getCpuInfo() throws IOException, InterruptedException
   {
      File file = new File("/proc/stat");
      BufferedReader br = new BufferedReader(new InputStreamReader(
      new FileInputStream(file)));
      StringTokenizer token = new StringTokenizer(br.readLine());
      token.nextToken();
      int user1 = Integer.parseInt(token.nextToken());
      int nice1 = Integer.parseInt(token.nextToken());
      int sys1 = Integer.parseInt(token.nextToken());
      int idle1 = Integer.parseInt(token.nextToken());
   
      Thread.sleep(1000);

      br = new BufferedReader(
      new InputStreamReader(new FileInputStream(file)));
      token = new StringTokenizer(br.readLine());
      token.nextToken();
      int user2 = Integer.parseInt(token.nextToken());
      int nice2 = Integer.parseInt(token.nextToken());
      int sys2 = Integer.parseInt(token.nextToken());
      int idle2 = Integer.parseInt(token.nextToken());

      return (float)((user2 + sys2 + nice2) - (user1 + sys1 + nice1)) / (float)((user2 + nice2 + sys2 + idle2) - (user1 + nice1 + sys1 + idle1));
   }
}

/**
* 测试类
*
* <p>@author javer QQ:84831612</p>
* @date 2005
*/
public class JaverTest
{
    public static void main(String args) throws Exception
    {
        int memInfo = LinuxSystemTool.getMemInfo();
        System.out.println("MemTotal:" + memInfo[0]);
        System.out.println("MemFree:" + memInfo);
        System.out.println("SwapTotal:" + memInfo);
        System.out.println("SwapFree:" + memInfo);

        System.out.println("CPU利用率:" + LinuxSystemTool.getCpuInfo());
    }
}

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号