How to show the CPU and disk usage in a JProgressBar?

早过忘川 提交于 2019-12-14 03:21:42

问题


I want to display the current cup usage and disk usage in two separate JProgressBars. And also it should update second by second. How to do it using java? I am quite new to java programming, so please give me some code example.

Thanks in advance.


回答1:


You can use javasysmon-0.3.4.jar file to get the system related informations as CPU Usage,memory usage etc.

Some of the methods are listed below and if you want something else you can explore.

                       JavaSysMon monitor=new JavaSysMon();
          //System.out.println("Operating System name is
                      "+monitor.osName());


           long usersMillis = monitor.cpuTimes().getUserMillis();
           /*System.out.println(String.format("User milli are %d days, %d
                        hr, %d min, %d sec", 
            TimeUnit.MILLISECONDS.toDays(usersMillis),
            TimeUnit.MILLISECONDS.toHours(usersMillis),
            TimeUnit.MILLISECONDS.toMinutes(usersMillis),
               TimeUnit.MILLISECONDS.toSeconds(usersMillis) 
           ));*/

          long systemMillis = monitor.cpuTimes().getSystemMillis();
          /*System.out.println(String.format("System milli are %d days, %d
                         hr, %d min, %d sec", 
            TimeUnit.MILLISECONDS.toDays(systemMillis),
            TimeUnit.MILLISECONDS.toHours(systemMillis),
            TimeUnit.MILLISECONDS.toMinutes(systemMillis),
               TimeUnit.MILLISECONDS.toSeconds(systemMillis) 
           ));*/

          long idleMilli = monitor.cpuTimes().getIdleMillis();
          /*System.out.println(String.format("Idle milli are %d days, %d
                       hr, %d min, %d sec", 
            TimeUnit.MILLISECONDS.toDays(idleMilli),
            TimeUnit.MILLISECONDS.toHours(idleMilli),
            TimeUnit.MILLISECONDS.toMinutes(idleMilli),
               TimeUnit.MILLISECONDS.toSeconds(idleMilli) 
           ));*/

          /*CpuTimes time=new CpuTimes(usersMillis, systemMillis,
                        idleMilli);
          cpuUsage=String.format("%.5f",
                       monitor.cpuTimes().getCpuUsage(time));*/
         // System.out.println("CPU Usages "+String.format("%.5f",
                        monitor.cpuTimes().getCpuUsage(time)));

          if(initialTime == null){
              initialTime = monitor.cpuTimes();
          }

          cpuUsage = new
                      Float(monitor.cpuTimes().getCpuUsage(initialTime)).toString();
          initialTime = monitor.cpuTimes();


          long cpuUpTimeL = monitor.uptimeInSeconds()*1000;

          float t = cpuUpTimeL/(float)(1000 * 60 * 60 * 24);
          int days=(int) t;
          t=t-days;
          t= (t*24);
          int hrs=(int) t;
          t=t-hrs;
          t=t*60;
          int mins=(int) t;
          t=t-mins;
          t=t*60;
          int secs=(int) t;
          cpuUpTime=GetTotalTimeTakenInJourney.getTotalTimeTaken(days, hrs,
                      mins, secs);
         // System.out.println("CPU Up time "+days+" days "+hrs+" hrs
                      "+mins+" mints "+secs+" seconds");

          //System.out.println("CPU Numbers "+monitor.numCpus());

          long totalBytes=monitor.physical().getTotalBytes();
          totalRam=totalBytes/(float)(1024*1024*1024);
          long freeBytes=monitor.physical().getFreeBytes();
          freeRam=freeBytes/(float)(1024*1024*1024);
          long ramUsages=totalBytes-freeBytes;
          usedRam=ramUsages/(float)(1024*1024*1024);



回答2:


You Could use tasklist command via this code

Runtime.getRuntime().exec(command)


来源:https://stackoverflow.com/questions/24426827/how-to-show-the-cpu-and-disk-usage-in-a-jprogressbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!