Skip to content

Instantly share code, notes, and snippets.

@franz1981
Created July 25, 2023 11:10
Show Gist options
  • Save franz1981/5e333f3b230fc3513492b95fcae6a1ce to your computer and use it in GitHub Desktop.
Save franz1981/5e333f3b230fc3513492b95fcae6a1ce to your computer and use it in GitHub Desktop.
package red.hat.puzzles.concurrent;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@State(Scope.Benchmark)
@Measurement(iterations = 5, time = 1)
@Warmup(iterations = 10, time = 1)
@Fork(value = 2, jvmArgsAppend = "--enable-preview")
public class VirtualThreadCreation {
@Param("10")
int work;
@Param("1024")
int tasks;
Executor executor;
@Setup
public void init() {
executor = Executors.newVirtualThreadPerTaskExecutor();
}
@Benchmark
public void submitWork() throws InterruptedException {
var done = new CountDownLatch(tasks);
for (int i = 0; i < tasks; i++) {
executor.execute(() -> {
Blackhole.consumeCPU(work);
done.countDown();
});
}
done.await();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment