Threading: replace CountdownLatch with CompletableFuture

This commit is contained in:
Sean Gilligan 2022-03-05 10:30:28 -08:00 committed by Andreas Schildbach
parent 11d676d103
commit 8ec6c05c6b

View File

@ -59,13 +59,15 @@ public class Threading {
* Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all
* tasks submitted before this point are now completed. Usually you won't want to use this method - it's a
* convenience primarily used in unit testing. If you want to wait for an event to be called the right thing
* to do is usually to create a {@link com.google.common.util.concurrent.SettableFuture} and then call set
* on it. You can then either block on that future, compose it, add listeners to it and so on.
* to do is usually to create a {@link CompletableFuture} and then call {@link CompletableFuture#complete(Object)}
* on it. For example:
* <pre>{@code
* CompletableFuture f = CompletableFuture.supplyAsync(() -> event, USER_THREAD)
* }</pre>
* You can then either block on that future, compose it, add listeners to it and so on.
*/
public static void waitForUserCode() {
final CountDownLatch latch = new CountDownLatch(1);
USER_THREAD.execute(() -> latch.countDown());
Uninterruptibles.awaitUninterruptibly(latch);
CompletableFuture.runAsync(() -> {}, USER_THREAD).join();
}
/**