Skip to content

Instantly share code, notes, and snippets.

@dhinojosa
Created November 14, 2023 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhinojosa/b9412d497e02977822ef5c523848b08b to your computer and use it in GitHub Desktop.
Save dhinojosa/b9412d497e02977822ef5c523848b08b to your computer and use it in GitHub Desktop.
Hot damn
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.atomic.AtomicLong;
public class MillionsOfTasks {
private final AtomicLong atomicLong;
public MillionsOfTasks() {
atomicLong = new AtomicLong();
}
public void submit() throws InterruptedException, ExecutionException {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
for (int i = 0; i < 1000000; i++) {
scope.fork(() -> System.out.printf("Freddy Rules - %d on Thread[%s]%n", atomicLong.incrementAndGet(), Thread.currentThread()));
}
scope.join().throwIfFailed();
System.out.println("Complete");
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
MillionsOfTasks millionsOfTasks = new MillionsOfTasks();
millionsOfTasks.submit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment