1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.common.cpu;
22
23 import java.lang.management.ManagementFactory;
24 import java.lang.management.OperatingSystemMXBean;
25
26
27
28
29
30 public class CpuManagement implements CpuManagementInterface {
31 OperatingSystemMXBean osBean;
32
33
34
35
36
37
38 public CpuManagement() throws UnsupportedOperationException {
39 osBean = ManagementFactory.getOperatingSystemMXBean();
40 if (osBean.getSystemLoadAverage() < 0) {
41 osBean = null;
42 throw new UnsupportedOperationException(
43 "System Load Average not supported");
44 }
45 }
46
47
48
49
50
51 public double getLoadAverage() {
52 return osBean.getSystemLoadAverage();
53 }
54
55 public static void main(String[] args) {
56 long total = 0;
57 CpuManagement cpuManagement = new CpuManagement();
58 System.err.println("LA: " + cpuManagement.getLoadAverage());
59 for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
60
61
62
63
64 total += i;
65 total *= 10;
66 }
67 System.err.println("LA: " + cpuManagement.getLoadAverage());
68
69 total = 0;
70 for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
71
72
73
74
75 total += i;
76 total *= 10;
77 }
78 System.err.println("LA: " + cpuManagement.getLoadAverage());
79 try {
80 Thread.sleep(10000);
81 } catch (InterruptedException e) {
82 }
83 System.err.println("LA: " + cpuManagement.getLoadAverage());
84
85 total = 0;
86 for (int i = 0; i < 1000 * 1000 * 1000; i ++) {
87
88
89
90
91 total += i;
92 total *= 10;
93 }
94 System.err.println("LA: " + cpuManagement.getLoadAverage());
95 }
96 }