Skip to content

Instantly share code, notes, and snippets.

@mmimica
Last active April 8, 2019 17:04
Show Gist options
  • Save mmimica/6981ccc0603901f7f3da6d70e4062886 to your computer and use it in GitHub Desktop.
Save mmimica/6981ccc0603901f7f3da6d70e4062886 to your computer and use it in GitHub Desktop.
public class WindowedInvoker<T, R> {
private final Semaphore semaphore = new Semaphore(windowSize);
private class ReleasingCallback<R> implements Consumer<R> {
private final Consumer<R> delegate;
@Override
public void accept(R result) {
semaphore.release();
delegate.accept(t);
}
}
public void invoke(Consumer<List<T>> consumer, List<T> batch, Consumer<R> callback) {
semaphore.acquire();
consumer.accept(batch, new ReleasingCallback(semaphore, callback));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment