Update ParallelUtils.java and IOBiConsumer.java

This commit is contained in:
Andrea Cavalli 2020-11-17 13:10:05 +01:00
parent a636411261
commit c64a272bd1
2 changed files with 108 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package org.warp.commonutils.batch;
import java.io.IOException;
import java.util.concurrent.CompletionException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
@ -8,6 +9,9 @@ import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.warp.commonutils.concurrency.executor.BlockingOnFullQueueExecutorServiceDecorator;
import org.warp.commonutils.concurrency.executor.BoundedExecutorService;
import org.warp.commonutils.functional.IOBiConsumer;
import org.warp.commonutils.functional.IOConsumer;
import org.warp.commonutils.functional.IOTriConsumer;
import org.warp.commonutils.functional.TriConsumer;
import org.warp.commonutils.type.IntWrapper;
import org.warp.commonutils.type.ShortNamedThreadFactory;
@ -15,6 +19,38 @@ import org.warp.commonutils.type.VariableWrapper;
public class ParallelUtils {
public static <V> void parallelizeIO(IOConsumer<IOConsumer<V>> iterator,
int maxQueueSize,
int parallelism,
int groupSize,
IOConsumer<V> consumer) throws IOException {
Consumer<Consumer<V>> action = (cons) -> {
try {
iterator.consume(cons::accept);
} catch (IOException e) {
throw new CompletionException(e);
}
};
try {
parallelize(action, maxQueueSize, parallelism, groupSize, (v) -> {
try {
consumer.consume(v);
} catch (IOException ex) {
throw new CompletionException(ex);
}
});
} catch (CompletionException ex) {
if (ex.getCause() instanceof CompletionException && ex.getCause().getCause() instanceof IOException) {
throw (IOException) ex.getCause().getCause();
} else if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause();
} else {
throw new IOException(ex);
}
}
}
public static <V> void parallelize(Consumer<Consumer<V>> iterator,
int maxQueueSize,
int parallelism,
@ -94,6 +130,38 @@ public class ParallelUtils {
}
}
public static <K, V> void parallelizeIO(IOConsumer<IOBiConsumer<K, V>> iterator,
int maxQueueSize,
int parallelism,
int groupSize,
IOBiConsumer<K, V> consumer) throws IOException {
Consumer<BiConsumer<K, V>> action = (cons) -> {
try {
iterator.consume(cons::accept);
} catch (IOException e) {
throw new CompletionException(e);
}
};
try {
parallelize(action, maxQueueSize, parallelism, groupSize, (k, v) -> {
try {
consumer.consume(k, v);
} catch (IOException ex) {
throw new CompletionException(ex);
}
});
} catch (CompletionException ex) {
if (ex.getCause() instanceof CompletionException && ex.getCause().getCause() instanceof IOException) {
throw (IOException) ex.getCause().getCause();
} else if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause();
} else {
throw new IOException(ex);
}
}
}
public static <K, V> void parallelize(Consumer<BiConsumer<K, V>> iterator,
int maxQueueSize,
int parallelism,
@ -184,6 +252,38 @@ public class ParallelUtils {
}
}
public static <K1, K2, V> void parallelizeIO(IOConsumer<IOTriConsumer<K1, K2, V>> iterator,
int maxQueueSize,
int parallelism,
int groupSize,
IOTriConsumer<K1, K2, V> consumer) throws IOException {
Consumer<TriConsumer<K1, K2, V>> action = (cons) -> {
try {
iterator.consume(cons::accept);
} catch (IOException e) {
throw new CompletionException(e);
}
};
try {
parallelize(action, maxQueueSize, parallelism, groupSize, (k1, k2, v) -> {
try {
consumer.accept(k1, k2, v);
} catch (IOException ex) {
throw new CompletionException(ex);
}
});
} catch (CompletionException ex) {
if (ex.getCause() instanceof CompletionException && ex.getCause().getCause() instanceof IOException) {
throw (IOException) ex.getCause().getCause();
} else if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause();
} else {
throw new IOException(ex);
}
}
}
public static <K1, K2, V> void parallelize(Consumer<TriConsumer<K1, K2, V>> iterator,
int maxQueueSize,
int parallelism,

View File

@ -0,0 +1,8 @@
package org.warp.commonutils.functional;
import java.io.IOException;
public interface IOBiConsumer<T, U> {
void consume(T t, U u) throws IOException;
}