Skip to content

Instantly share code, notes, and snippets.

@isopov
Last active May 29, 2022 11:03
Show Gist options
  • Save isopov/292725fe49b196645a2fb554aa2e0aae to your computer and use it in GitHub Desktop.
Save isopov/292725fe49b196645a2fb554aa2e0aae to your computer and use it in GitHub Desktop.
Loom non time sharing test
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.concurrent.*;
public class LoomTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
long platform = 0;
long loom = 0;
for (int i = 0; i < 5; i++) {
platform += report(Thread.ofPlatform().factory());
loom += report(Thread.ofVirtual().factory());
}
System.out.println("loom " + TimeUnit.NANOSECONDS.toMillis(loom));
System.out.println("platform " + TimeUnit.NANOSECONDS.toMillis(platform));
}
private static final int TASKS = 1_000;
private static long report(ThreadFactory threadFactory) throws ExecutionException, InterruptedException {
var threads = new ArrayList<Thread>(2 * TASKS);
var lights = new ArrayList<LightWorker>(TASKS);
for (int i = 0; i < TASKS; i++) {
Thread thread = threadFactory.newThread(new HeavyWorker());
threads.add(thread);
thread.start();
}
for (int i = 0; i < TASKS; i++) {
LightWorker light = new LightWorker();
lights.add(light);
Thread thread = threadFactory.newThread(light);
threads.add(thread);
thread.start();
}
for (var thread : threads) {
thread.join();
}
long result = 0;
for (var light : lights) {
result += light.time;
}
return result;
}
private static class LightWorker implements Runnable {
private final long start;
private long time;
private LightWorker() {
this.start = System.nanoTime();
}
@Override
public void run() {
time = System.nanoTime() - start;
}
}
public static long blackHole;
private static class HeavyWorker implements Runnable {
public void run() {
BigInteger res = BigInteger.ZERO;
for (int j = 0; j < 1_000; j++) {
res = res.add(BigInteger.valueOf(1L));
}
blackHole = res.longValue();
}
}
}
loom 21686
platform 313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment