Created
November 14, 2023 21:58
-
-
Save dhinojosa/b9412d497e02977822ef5c523848b08b to your computer and use it in GitHub Desktop.
Hot damn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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