Код: Выделить всё
class Test {
int waitSec=0;
String className;
public Test(int waitSec, String className) {
this.waitSec = waitSec;
this.className = className;
}
public void mymethod() throws Exception {
System.out.println("Executing mymethod in "+className);
System.out.println("before "+className);
if(waitSec != 0) {
new CompletableFuture().completeAsync(() -> null,
CompletableFuture.delayedExecutor(10, TimeUnit.SECONDS)).join();
}
if(waitSec == -1) {
throw new Exception("aaa"+className);
}
System.out.println("after"+className);
}
}
public class Main2 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ArrayList[*]> list= new ArrayList();
List myTest1 = new ArrayList();
myTest1.add(new Test(10,"Class1"));
myTest1.add(new Test(0, "Class2"));
List myTest2 = new ArrayList();
myTest2.add(new Test(0, "Class3"));
myTest2.add(new Test(0, "Class4"));
list.add(myTest1);
list.add(myTest2);
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
List cp = new ArrayList();
for (List myTest : list) {
for (Test test : myTest) {
CompletableFuture future = CompletableFuture.runAsync(() -> {
try {
test.mymethod();
} catch (Exception e) {
System.out.print(">>>>>>>>>>>>>>>>exception>>>>timeout
Подробнее здесь: [url]https://stackoverflow.com/questions/78356018/after-completable-future-timeout-and-exceptions-program-keeps-executing[/url]