gpt4 book ai didi

java - 发生TimeoutException时JVM不退出

转载 作者:行者123 更新时间:2023-12-01 19:24:04 25 4
gpt4 key购买 nike

我有代码需要做这样的事情

有一个类列表,每个类都有一些方法(比如说execute())。我需要在每个类上调用该方法,并且每次调用都有固定的超时。现在,类的其中一个执行方法写得不好,导致超时,导致 jvm 不退出。我就是这样管理类(class)的。

java ExecutorServiceTest execute TestClass1 TestClass2 TestClass3

为什么jvm执行完代码后不退出?

我得到以下输出

In class 1
In Class 2
java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at ExecutorServiceTest.main(ExecutorServiceTest.java:78)
java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)

第二个类的执行超时,之后第三个类的执行也超时。为什么第三个类执行超时?

执行完成后jvm不会退出。是什么原因?另外为什么 TestClass3 执行超时?

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

class Task implements Callable<String> {
Object instance;
Method m;
Object[] input;
Task(Object instance, Method m, Object[] input) {
this.instance = instance;
this.m = m;
this.input = input;
}
public String call() {
String s = "initial";
try {
m.invoke(instance, input);
}
catch (RuntimeException e) {
}
catch (Exception e) {
}
finally {

}
return s;
}
}


public class ExecutorServiceTest {
public static void main(String[] args) {
String methodName = args[0];
String className;
List<Object> instanceList = new ArrayList<Object>();
for (int i=1;i<args.length;i++) {
className = args[i];
Object o = null;
try {
o = Class.forName(className).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
instanceList.add(o);
}
ExecutorService executor = Executors.newSingleThreadExecutor();
Iterator<Object> iter = instanceList.iterator();
while (iter.hasNext()) {
Object o = iter.next();
Method m = null;
try {
m = o.getClass().getDeclaredMethod(methodName, new Class[] {});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Task t = new Task(o,m,new Object[]{});
Future<String> fut = executor.submit(t);
try {
fut.get(2,TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
executor.shutdown();
}
}


public class TestClass1 {
public void execute() {
System.out.println("In class 1");
}
}


public class TestClass2 {
public void execute() {
System.out.println("In class 2");
boolean b = true;
while (b) {

}
}
}


public class TestClass3 {
public void execute() {
System.out.println("In class 3");
}
}

最佳答案

ExecutorService.shutdown() 实际上并没有停止任何正在运行的执行程序/线程,it just tells the service to stop accepting new tasks :

void shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

您的 TestClass2 实例永远不会停止运行,因为它有一个永远不会停止的 while(true) 循环。

如果想立即停止ExecutorService,可以使用awaitTermination(long timeout, TimeUnit unit)shutdownNow()

关于java - 发生TimeoutException时JVM不退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2419611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com