Skip to content

Commit

Permalink
Make methods synchronized; move check for zero; rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljmarshall committed Feb 10, 2021
1 parent 7a53be0 commit fb87dee
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ public SystemResourceUsage getBrokerHostUsage() {
return usage;
}

private void checkCpuLoad() {
synchronized (this) {
cpuUsageSum += systemBean.getSystemCpuLoad();
cpuUsageCount++;
}
private synchronized void checkCpuLoad() {
cpuUsageSum += systemBean.getSystemCpuLoad();
cpuUsageCount++;
}

@Override
Expand All @@ -91,19 +89,17 @@ private double getTotalCpuLimit() {
return 100 * Runtime.getRuntime().availableProcessors();
}

private double getTotalCpuUsage() {
synchronized (this) {
double cpuUsage = cpuUsageSum / cpuUsageCount;
cpuUsageSum = 0d;
this.cpuUsageCount = 0;
return cpuUsage;
private synchronized double getTotalCpuUsage() {
if (cpuUsageCount == 0) {
return 0;
}
double cpuUsage = cpuUsageSum / cpuUsageCount;
cpuUsageSum = 0d;
cpuUsageCount = 0;
return cpuUsage;
}

private ResourceUsage getCpuUsage() {
if (cpuUsageCount == 0) {
return new ResourceUsage(0, totalCpuLimit);
}
return new ResourceUsage(getTotalCpuUsage() * totalCpuLimit, totalCpuLimit);
}

Expand Down

0 comments on commit fb87dee

Please sign in to comment.