gpt4 book ai didi

java - Thread.sleep() 没有暂停执行

转载 作者:行者123 更新时间:2023-11-30 10:44:23 26 4
gpt4 key购买 nike

我试图通过在 20 次尝试中每个循环 hibernate 3,000 毫秒来将循环中进程的尝试限制为 60 秒。调用 Thread.sleep() 实际上并不是在正在运行的线程中暂停执行,而是所有 20 次尝试都快速连续发生。

private void pollWebServiceForToken() {
final int pollInterval = 3000;
new Thread() {
@Override
public void run() {
int attempts = 0;
int maxAttempts = 60;
String token;
do {
token = requestToken(exchangeCode);
if (token.contains(FAILED)) {
try {
Thread.sleep(pollingInterval);
} catch (InterruptedException ex) {
this.currentThread().interrupt();
}
}
attempts++;
} while (token.toLowerCase().contains(FAILED) && attempts < maxAttempts && !cancelled);
}
}.start();
}

由于这一切都发生在 Vaadin 应用程序内部,我假设错误的线程正在进入 hibernate 状态,但我不确定如何针对特定线程进行 hibernate 。

提前致谢

最佳答案

您确定 if 中的代码正在运行吗? while 的条件不同 (+toLowerCase)。 Thread.sleep() 总是导致当前线程 hibernate 。

关于java - Thread.sleep() 没有暂停执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402656/

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