Skip to content

Instantly share code, notes, and snippets.

@rea9r
Last active April 7, 2024 14:22
Show Gist options
  • Save rea9r/4984b2a23f9d91d522dc0cd778a899a0 to your computer and use it in GitHub Desktop.
Save rea9r/4984b2a23f9d91d522dc0cd778a899a0 to your computer and use it in GitHub Desktop.
CpuLoadDemo.java
package org.example;
public class CpuLoadDemo {
public static void main(String[] args) {
int processors = Runtime.getRuntime().availableProcessors();
System.out.println("Available processors: " + processors);
for (int i = 0; i < processors; i++) {
new Thread(() -> {
while (true) {
heavyTask();
}
}, "HeavyTaskThread").start();
new Thread(() -> {
while (true) {
lightTask();
}
}, "LightTaskThread").start();
}
}
private static void heavyTask() {
double value = 0;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
value += Math.sin(i) * Math.cos(i);
}
System.out.println("Heavy Task Done: " + value);
}
private static void lightTask() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Light Task Done");
}
}
@rea9r
Copy link
Author

rea9r commented Apr 7, 2024

スクリーンショット 2024-04-07 22 56 29

@rea9r
Copy link
Author

rea9r commented Apr 7, 2024

スクリーンショット 2024-04-07 23 22 38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment