我正在练习 Cay S. Horstmann 所著的“真正不耐烦的 Java SE 8”一书。
写一个方法
public static <T> CompletableFuture<T> repeat( Supplier<T> action, Predicate<T> until)
异步重复操作,直到它产生一个函数接受的值,该
until
函数也应该异步运行。使用从控制台读取 ajava.net.PasswordAuthentication
的函数以及通过休眠一秒钟然后检查密码是否为“秘密”来模拟有效性检查的函数进行测试。
我想出了以下代码,但随机密码生成策略似乎让我失望了。所有线程不断选择相同的密码,这看起来很奇怪。
public static <T> CompletableFuture<T> repeat(final Supplier<T> action, final Predicate<T> until) {
final CompletableFuture<T> futureAction = supplyAsync(action);
final boolean isMatchFound = futureAction.thenApplyAsync(until::test).join();
final T suppliedValue = getSuppliedValue(futureAction);
if (isMatchFound) {
LOGGER.info("Got a match for value {}.", suppliedValue);
return futureAction;
}
return repeat(() -> suppliedValue, until);
}
private static <T> T getSuppliedValue(final CompletableFuture<T> futureAction) {
try {
return futureAction.get();
} catch (InterruptedException | ExecutionException e) {
LOGGER.error(e.getMessage());
}
return null;
}
测试用例:
@Test
public void testRepeat() {
Supplier<PasswordAuthentication> action = () -> {
final String[] passwordVault = new String[] { "password", "secret",
"secretPassword" };
final int len = passwordVault.length;
return new PasswordAuthentication("mickeyMouse",
passwordVault[ThreadLocalRandom.current().nextInt(len)].toCharArray());
};
@SuppressWarnings("static-access")
Predicate<PasswordAuthentication> until = passwordAuth -> {
try {
currentThread().sleep(1000);
} catch (InterruptedException e) {
fail(e.getMessage());
}
final String password = String.valueOf(passwordAuth.getPassword());
LOGGER.info("Received password: {}.", password);
return password.equals("secret");
};
repeat(action, until);
}
运行一次,看看选择相同的密码有多奇怪:
2015-01-09 15:41:33.350 [Thread-1] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:34.371 [Thread-3] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:35.442 [Thread-5] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:36.443 [Thread-7] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:37.451 [Thread-9] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。