Skip to content

Commit

Permalink
connection manager switches to better connection, integrates with wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Sep 7, 2023
1 parent a8bcd02 commit f24e153
Show file tree
Hide file tree
Showing 13 changed files with 597 additions and 249 deletions.
46 changes: 42 additions & 4 deletions src/main/java/common/utils/GenUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -393,7 +395,7 @@ public static String getUUID() {
/**
* Wait for the duration.
*
* @param durationMs is the duration to wait for in milliseconds
* @param durationMs the duration to wait for in milliseconds
*/
public static void waitFor(long durationMs) {
try { TimeUnit.MILLISECONDS.sleep(durationMs); }
Expand All @@ -403,7 +405,7 @@ public static void waitFor(long durationMs) {
/**
* Execute tasks in parallel.
*
* @param tasks are the tasks to execute in parallel
* @param tasks are tasks to execute in parallel
*/
public static void executeTasks(Collection<Runnable> tasks) {
executeTasks(tasks, tasks.size());
Expand All @@ -412,8 +414,8 @@ public static void executeTasks(Collection<Runnable> tasks) {
/**
* Execute tasks in parallel.
*
* @param tasks are the tasks to execute in parallel
* @param maxConcurrency is the maximum number of tasks to run in parallel
* @param tasks are tasks to execute in parallel
* @param maxConcurrency the maximum number of tasks to run in parallel
*/
public static void executeTasks(Collection<Runnable> tasks, int maxConcurrency) {
if (tasks.isEmpty()) return;
Expand All @@ -435,4 +437,40 @@ public static void executeTasks(Collection<Runnable> tasks, int maxConcurrency)
throw new RuntimeException(e);
}
}

/**
* Execute tasks in parallel.
*
* @param <T> parameterized type
* @param tasks the tasks to execute in parallel
* @return the results of each task
* @throws InterruptedException
* @throws ExecutionException
*/
public <T> List<T> executeTasks(List<Callable<T>> tasks) throws InterruptedException, ExecutionException {
return executeTasks(tasks, tasks.size());
}

/**
* Execute tasks in parallel.
*
* @param <T> parameterized type
* @param tasks the tasks to execute in parallel
* @param maxConcurrency the maximum number of tasks to run in parallel
* @return the results of each task
* @throws InterruptedException
* @throws ExecutionException
*/
public <T> List<T> executeTasks(List<Callable<T>> tasks, int maxConcurrency) throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(maxConcurrency);
List<Future<T>> futures = new ArrayList<>();
List<T> results = new ArrayList<>();
try {
for (Callable<T> task : tasks) futures.add(executor.submit(task));
for (Future<T> future : futures) results.add(future.get());
} finally {
executor.shutdown();
}
return results;
}
}
Loading

0 comments on commit f24e153

Please sign in to comment.