目标是接收计算的第一个结果。但是,我看到是否有日志,只有一个线程启动。怎么解决?
ExecutorService executor = Executors.newFixedThreadPool(3, new ThreadFactory() {
int count = 1;
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "my thread-" + count++);
}
});
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (findDecodedString(hash)) {
return answer;
}
return null;
},executor);
CompletableFuture<Object> result = CompletableFuture.anyOf(future);
此函数称为:
private boolean findDecodedString(String str) {
System.out.println(Thread.currentThread().getName());
for (int lengthOfWord = 1; lengthOfWord <= 100; lengthOfWord++) {
if (find(lengthOfWord, str)) {
return true;
}
}
return false;
}
启动时,只有一个线程-0 启动。如何启动多个线程?当其中一个找到完成的计算时,如何停止所有线程?