-
-
Save rea9r/4984b2a23f9d91d522dc0cd778a899a0 to your computer and use it in GitHub Desktop.
CpuLoadDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
Author
rea9r
commented
Apr 7, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment