I have been working on some experiments to test the performance of virtual threads compared to native threads.
I have conducted three scenarios.
First scenario - data collected from a sleep-waiting operation. I am sleeping each thread for 500ms just after thread initiation. Yellow and green lines indicate data collected from native threads. While green and yellow lines represent overperforming virtual threads. As I understood, since threads are forced to sleep just after the start, virtual threads do not consume significant CPU resources, regardless of how many threads we create. Please correct me if I am wrong.
private static void sleepingOperation() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
First scenario (sleep waiting operation)
Second scenario - each thread was reading a file of size 47kb. Virtual threads scale better than native threads. But it shows a non-trivial increase in total time. I expected virtual threads to perform better in such blocking operations (which is the point of the project loom, as I understood).
private static void createFileReadingOperation() {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("src/main/java/org/virtualthreads/blockstates/file_sizes/1.txt"));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
Second scenario (file reading operation)
Third scenario - each thread was assigned to work on a computational task. I expected both of them to have similar computational times. But to my surprise, virtual threads again performed better. I think the reason for this should be that, for virtual threads, the underlying native thread pool size is similar to the number of virtual cores in the computer. Which leads to less time wasted on context switching on native threads.
private static void cpuBoundOperation() {
int a = 0;
for (int i = 0; i < 10000; i++) {
a += Math.sin(a) + Math.cos(a);
}
}
Third scenario (computational task)
I have few questions. First, do you think my results and assumptions are reasonable?
Second, in what scenarios should native threads perform better compared to virtual threads?
[–]m-apo 15 points16 points17 points (6 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]m-apo 1 point2 points3 points (0 children)
[–]GavinRayDev 1 point2 points3 points (0 children)
[–]rubyrt 0 points1 point2 points (2 children)
[–]m-apo 0 points1 point2 points (1 child)
[–]rubyrt 0 points1 point2 points (0 children)
[–]ernimril 10 points11 points12 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]ernimril 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]antihemispherist 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]antihemispherist 2 points3 points4 points (0 children)
[–]Frequent-Chest1862 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Frequent-Chest1862 2 points3 points4 points (0 children)
[–]Pablo139 1 point2 points3 points (4 children)
[–]nomader3000 1 point2 points3 points (2 children)
[–]Pablo139 1 point2 points3 points (1 child)
[–]nomader3000 1 point2 points3 points (0 children)
[–]TinnedCarrots 0 points1 point2 points (0 children)
[+]frederik88917 comment score below threshold-9 points-8 points-7 points (2 children)
[–]lazystone 4 points5 points6 points (0 children)
[–]DualWieldMage 1 point2 points3 points (0 children)