我有网络要求,迫使我将空闲请求发送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的循环而仍然休眠剩余时间?如果是,最好的方法是什么?
答案 0 :(得分:2)
如果我正确理解,您希望以某种方式处理InterruptedException
,以确保在执行sendRequest
之前没有延迟。好吧,由于InterruptedException
发生在线程中有人调用interrupt()
时,该catch块不会被执行,因此您不必担心。
您的方法不太正确,被动方法会更好。即使是基本的Timer也更好。