How to show the CPU and disk usage in a JProgressB

2019-09-18 13:31发布

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.

2条回答
Luminary・发光体
2楼-- · 2019-09-18 13:54

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);
查看更多
我想做一个坏孩纸
3楼-- · 2019-09-18 14:06

You Could use tasklist command via this code

Runtime.getRuntime().exec(command)
查看更多
登录 后发表回答