我有网络要求,迫使我闲置请求发送 2 秒
requester = new Thread(){
@Override
public void run(){
buildRequest();
Thread.sleep(requestDelay);
} catch( InterruptedException e){
// keep idle for the remaining time still
// interrupted at 1s need to sleep 1 second more
e.printStackTrace();
}
sendRequest();
}
}
是否仍然可以通过循环消耗 CPU 来 sleep 剩余时间?如果是的话是最好的方法吗?
最佳答案
如果我理解正确的话,你想处理 InterruptedException
以这种方式不确定执行之前的延迟 sendRequest
。好吧,自从 InterruptedException
当有东西调用 interrupt()
时发生在线程上,该 catch block 不会被执行,因此您不必担心。
你的方法不太正确,响应式(Reactive)方法会更好。即使是基本的Timer更好。
关于java - 当线程中断时继续 hibernate 剩余时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53711649/