]> Git Server - utils.git/commitdiff
1.4.0: added Parallel Processing
authorRobin Cheney <cheneyr@eternal.ddnss.de>
Tue, 25 Nov 2025 10:16:40 +0000 (11:16 +0100)
committerRobin Cheney <cheneyr@eternal.ddnss.de>
Tue, 25 Nov 2025 10:16:40 +0000 (11:16 +0100)
pom.xml
src/main/java/de/ddnss/eternal/utils/threading/ParallelProcessor.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index e1cfaaa1b3d91a7ba0d8ad0b550a418d0f77118d..8bab3be91c33eae817fda70801fcfb6151b2d689 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
 
     <groupId>de.ddnss.eternal.utils</groupId>
     <artifactId>utils</artifactId>
-    <version>1.3.3</version>
+    <version>1.4.0</version>
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
diff --git a/src/main/java/de/ddnss/eternal/utils/threading/ParallelProcessor.java b/src/main/java/de/ddnss/eternal/utils/threading/ParallelProcessor.java
new file mode 100644 (file)
index 0000000..cea151d
--- /dev/null
@@ -0,0 +1,41 @@
+package de.ddnss.eternal.utils.threading;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+
+public class ParallelProcessor {
+
+    public static void run(Runnable function, int threads) {
+        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads);
+        for (int i = 0; i < threads; i++) {
+            executor.submit(function);
+        }
+    }
+
+    /**
+     * Runs the given task repeatedly using the given number of threads until
+     * the provided condition becomes true.
+     */
+    public static void runUntil(Runnable task, int threads, Supplier<Boolean> stopCondition) {
+        ExecutorService executor = Executors.newFixedThreadPool(threads);
+
+        for (int i = 0; i < threads; i++) {
+            executor.submit(() -> {
+                while (!stopCondition.get()) {
+                    task.run();
+                }
+            });
+        }
+
+        executor.shutdown();
+
+        try {
+            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        }
+    }
+}