因此,我正在尝试在“玩具语言”上实现某种派生。就像实现我自己的编译器一样。因此,当用户进行fork时,我的程序应该启动/模拟一些线程。
在我的代码中,我准备了可调用对象,然后开始执行可调用对象,但是当我使用invokeAll时,我的程序没有终止,它什么也没做。
我通过debug运行了代码,当我进入invokeAll()时,它只是停止了调试,但是它没有终止或抛出错误或任何东西(我在try-catch内部拥有了invoke)。我也尝试了固定线程池。它什么也没做
一些代码:
// preparing the callables
java.util.List<Callable<MyClass>> callList = prgll.stream()
.map(p -> (Callable<MyClass>) () -> {
return p.oneStep(); //a method from my class
}).collect(Collectors.toList());
//start the execution of the callables
//it should return the list of new created threads
ExecutorService executor = Executors.newSingleThreadExecutor();
java.util.List<MyClass> fff;
try {
fff = executor.invokeAll(callList).stream() // here my program gets blocked but not all the time, only when I call use myFork class
.map(future-> {
try {
return future.get();
} catch (Exception e) {
e.printStackTrace();
throw new CustomException("Error in onestepforall" + e.getMessage());
}
}).filter(p->p != null).collect(Collectors.toList());
} catch (InterruptedException e) {
e.printStackTrace();
throw new CustomException("Error while trying executor" +e.getMessage());
}
我可以调试此代码以深入了解我的代码,以确切了解为什么invokeAll保持待机状态吗?
我还尝试将新的SingleThread更改为固定池,但仍然没有任何作用。
最佳答案
invokeAll()
是一种冻结方法,即等待所有期货完成。
如果要异步任务提交,则通过执行者的.map
submit()
可调用项列表。
如果您的问题是为什么任务没有完成,那么您不想调试提交给执行者的线程,而是调试生成的线程,因为任务是在单独的线程上执行的。无需使用断点,您可以简单地使用附加的调试器挂起整个JVM,然后查看各个线程。