Skip to content

Instantly share code, notes, and snippets.

@daniellansun
Created September 21, 2022 14:50
Show Gist options
  • Save daniellansun/e077a5f5bf395051718df881c0922812 to your computer and use it in GitHub Desktop.
Save daniellansun/e077a5f5bf395051718df881c0922812 to your computer and use it in GitHub Desktop.
import java.time.*
import java.util.concurrent.*
import java.util.concurrent.atomic.*
def atomicInteger = new AtomicInteger()
def runnable = () -> {
atomicInteger.incrementAndGet()
}
def start = Instant.now()
try (def executor = Executors.newVirtualThreadPerTaskExecutor()) {
for(int i = 0; i < 1_000_000; i++) {
executor.submit(runnable)
}
}
def finish = Instant.now()
def timeElapsed = Duration.between(start, finish).toMillis()
println "Result: ${atomicInteger}. Total elapsed time: ${timeElapsed}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment