gpt4 book ai didi

java - future - 线程未能停止

转载 作者:行者123 更新时间:2023-11-30 04:16:08 24 4
gpt4 key购买 nike

我正在使用以下代码(大纲):

ExecutorService executor = Executors.newFixedThreadPool(20);
List<Future<statusModel>> futures = new ArrayList<Future<statusModel>>();

for (Map.Entry<String, String> url : urls.entrySet())
{
Future<statusModel> future = executor.submit(mycallable);
futures.add(future);
}
for (Map.Entry<String, String> url : urls.entrySet())
{
try
{
status = (statusModel) futures.get(i).get(50, TimeUnit.MILLISECONDS);
// do stuff with status
}
catch (InterruptedException | ExecutionException | TimeoutException e)
{
System.out.println("Error<checkServers>: Timeout OR "+e.getMessage());
}
}

executor.shutdownNow();
System.out.println("Shutdown: "+executor.isShutdown());

我的控制台显示:关闭:true

myCallable:

public statusModel call() throws Exception 
{
InputStream in = null;
BufferedReader br = null;
statusModel status = new statusModel();

try
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

status.setStatusCode(conn.getResponseCode());
status.setUrl(urlStr);

if(status.getStatusCode()/100 == 2) // Status = OK
{ // Read JSON response }
}
catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(in != null)
in.close();
if(br != null)
br.close();
}

return status;
}

有时,当我一遍又一遍地运行此 block 时,我会收到此错误:

Aug 20, 2013 9:35:44 AM org.apache.catalina.core.StandardWrapper unload INFO: Waiting for 1 instance(s) to be deallocated Aug 20, 2013 9:35:45 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads SEVERE: The web application [/Server_Status] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. Aug 20, 2013 9:35:45 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads SEVERE: The web application [/Server_Status] appears to have started a thread named [pool-3-thread-6] but has failed to stop it. This is very likely to create a memory leak.

我已经关闭了“执行器”,也检查了同样的情况。我仍然收到此错误。我在这里做错了什么吗?

更新:我是第一次使用 Future。如果我需要发布更多内容以更好地解释,请告诉我。

更新:我尝试打印所有 future.isDone()。由于某种原因,超出超时时间的 future 仍然返回 isDone() = false - 没有被 timout 取消:(

感谢任何帮助。提前致谢。

最佳答案

shutdownNow() 终止所有仍在运行的作业,这可能是导致错误消息的原因。您可能需要 shutdown() 它不会让任何新作业提交,但让已排队的作业继续执行。

此外,您需要通过调用 future.get() 等待 futures 完成(如果 future 尚未完成,则可能会阻塞)以获取结果。

for(Future<statusModel> future : futures){
statusModel model = future.get();
//do stuff with statusModel
}

编辑:既然已经发布了附加代码,我已将其附加到我的答案中:

我已经重新检查了 javadoc 中的 future.get(long timeout, TimeUnit unit),它并没有说如果发生超时,那么 future 就会被取消。我还检查了 Brian Goetz 的《Java Concurrency in Practice》(顺便说一句,这是一本很棒的书;必读),在第 147 页上推荐了以下内容:

try{
future.get(timeout, unit);
}catch(TimeoutException e){
//task will be cancelled below
}catch(ExecutionException e){
//exception thrown in task; rethrow
//... throw new MyWrappedException(e.getCause());
}finally{
//Harmless if task already completed
future.cancel(true); //interrupt if still running
}

关于java - future - 线程未能停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18336782/

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