Created
November 20, 2019 17:36
-
-
Save TurpIF/2153f0575bfd42e025ff8ea7513a4acf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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