Java 从未来获得结果,而不必完成所有
我创建了10个线程,并向其中添加了2种作业,如下所示Java 从未来获得结果,而不必完成所有,java,multithreading,future,Java,Multithreading,Future,我创建了10个线程,并向其中添加了2种作业,如下所示 public class ParallelAdder { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(10); List<Futu
public class ParallelAdder {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(10);
List<Future<Integer>> list = new ArrayList<Future<Integer>>();
for (int i = 0; i < 10; i++) {
Future<Integer> future;
if (i % 2 == 0) {
future = executor.submit(new Call1());
} else {
future = executor.submit(new Call2());
}
list.add(future);
}
for(Future<Integer> fut : list) {
System.out.println("From future is "+fut.get());
}
}
}
class Call1 implements Callable<Integer> {
public Integer call() throws Exception {
System.out.println("Calling 1");
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 10;
}
}
class Call2 implements Callable<Integer> {
public Integer call() throws Exception {
System.out.println("Calling 2");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 20;
}
}
公共类并行加法器{
公共静态void main(字符串[]args)引发ExecutionException、InterruptedException{
ExecutorService executor=Executors.newFixedThreadPool(10);
列表=新的ArrayList();
对于(int i=0;i<10;i++){
未来;
如果(i%2==0){
future=executor.submit(newcall1());
}否则{
future=executor.submit(newcall2());
}
增加(未来);
}
对于(未来未来:列表){
System.out.println(“From future is”+fut.get());
}
}
}
类Call1实现了可调用的{
公共整数调用()引发异常{
System.out.println(“调用1”);
试一试{
睡眠(100000);
}捕捉(中断异常e){
e、 printStackTrace();
}
返回10;
}
}
类Call2实现了可调用的{
公共整数调用()引发异常{
System.out.println(“调用2”);
试一试{
睡眠(10);
}捕捉(中断异常e){
e、 printStackTrace();
}
返回20;
}
}
Call2作业的返回速度比Call1快。
在我未来的列表中,我希望作业一完成就得到结果。它不应依赖于所有其他工作来完成。这里Call2 return正在等待Call1。如何解决此问题?问题是您正在通过调用阻塞
get
来等待:
for(Future<Integer> fut : list) {
System.out.println("From future is "+fut.get());
}
以下只是为了确保当前线程不会在所有异步任务完成之前退出。但是如果它是一个长时间运行的应用程序,比如服务器,这可能是不必要的
for (CompletableFuture<Void> future : futures) {
future.join();
}
for(可完成的未来:未来){
future.join();
}
注意,我内联了
Call1
和Call2
中的代码,因为Callable
实现不是必需的。但最好将其保存在单独的类中(除非函数对象还可以)。谢谢!。但如果我删除future.join();然后它也开始工作了fine@oxy_js哦,是的,它可能仍然有效。我只是稍微编辑了一下,并评论说,如果您是从长时间运行的应用程序(如服务器)调用它,则不需要这样做。只是要注意是否从main
运行它,因为main方法可能会终止应用程序,从而导致VM在异步任务完成运行之前退出。现在我明白了。再次感谢
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
CompletableFuture<Void> future =
CompletableFuture.supplyAsync(call1Supplier, executor)
.thenAccept(number -> System.out.println("From future is " + number));
futures.add(future);
} else {
CompletableFuture<Void> future =
CompletableFuture.supplyAsync(call2Supplier, executor)
.thenAccept(number -> System.out.println("From future is " + number));
futures.add(future);
}
}
for (CompletableFuture<Void> future : futures) {
future.join();
}