Skip to content

Instantly share code, notes, and snippets.

@TurpIF
Created November 20, 2019 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TurpIF/2153f0575bfd42e025ff8ea7513a4acf to your computer and use it in GitHub Desktop.
Save TurpIF/2153f0575bfd42e025ff8ea7513a4acf to your computer and use it in GitHub Desktop.
public class TrackingCommandsExecutor implements Executor {
private final Queue<CountDownLatch> commandLatches = new ConcurrentLinkedQueue<>();
private final Executor delegate;
public TrackingCommandsExecutor(Executor delegate) {
this.delegate = delegate;
}
@Override
public void execute(@NonNull Runnable command) {
CountDownLatch latch = new CountDownLatch(1);
commandLatches.add(latch);
Runnable trackedCommand = () -> {
command.run();
latch.countDown();
};
delegate.execute(trackedCommand);
}
public void waitCommands() throws InterruptedException {
CountDownLatch latch;
while ((latch = commandLatches.poll()) != null) {
try {
latch.await();
} catch (InterruptedException e) {
commandLatches.add(latch);
throw e;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment