Skip to content

Commit

Permalink
wait
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuangcp committed Sep 13, 2024
1 parent b7e68c3 commit b4e9fb8
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -142,14 +143,32 @@ private int func2(int value) {
*/
@Test
public void testSpinWait() throws Exception {
CompletableFuture<Boolean> first = CompletableFuture.supplyAsync(this::enableRun);
log.info("start");
CompletableFuture<Void> first = CompletableFuture.runAsync(() -> {
boolean enable = this.enableRun();
while (!enable) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException ignored) {
}
enable = enableRun();
}
});


try {
first.get(5, TimeUnit.SECONDS);
} catch (TimeoutException e) {
log.error("timeout", e);
}
log.info("finish");
// first.thenCombine();
}

private final LocalTime now = LocalTime.now();

private boolean enableRun() {
LocalTime now1 = LocalTime.now();
return now1.isAfter(now.plusMinutes(1));
return now1.isAfter(now.plusSeconds(3));
}
}

0 comments on commit b4e9fb8

Please sign in to comment.