Java SpringWebApp关闭和终止线程
甲级 B类Java SpringWebApp关闭和终止线程,java,multithreading,spring,Java,Multithreading,Spring,甲级 B类 public Data getData(final String id){ if(notAvailable(id)){ classB.doSomethingAsync(); throw new NotAvailableException(); } else { return getData(id); } } 假设所有这些都是在web应用程序中运行的。发出请求并调用ClassA.getData()方法。代码路径遵循n
public Data getData(final String id){
if(notAvailable(id)){
classB.doSomethingAsync();
throw new NotAvailableException();
}
else {
return getData(id);
}
}
假设所有这些都是在web应用程序中运行的。发出请求并调用ClassA.getData()方法。代码路径遵循notAvailable()返回true的情况,我们调用ClassB.doSomethingAsync()并从ClassA向客户端抛出异常
我的问题是,当web应用程序关闭时,类B中的while循环是否会在不等待计数达到零的情况下过早退出
在测试过程中,我看到的结果好坏参半,我希望有人能给出确切的答案。该应用程序正在Jetty下运行。您看到的一些混合结果是什么?很抱歉,回复太晚。我看到过应用程序关闭的情况,就像线程被中断一样,有时循环继续执行,直到计数达到零。我在想,当then interrupt发生时,关机到达了循环处于5秒睡眠(Thread.sleep)的时间点。我想知道B类是否应该通过@PreDestroy钩住应用程序的关闭,在这里一个字段(即isShuttingDown)可以设置为true,循环可以在每次迭代中检查这个字段。
@Async
public void doSomethingAsync()
{
int count = 100;
while (count > 0)
{
count--;
System.out.println(times);
try
{
Thread.sleep(5000);
}
catch (final InterruptedException e)
{
System.out.println("Sleep interrupted. Exiting");
return;
}
}
}