我正在制作一个 boolean
,因此如果满足特定条件,它将返回 true 3 秒,但之后它会再次返回 false。
原来如此
private boolean timedBoolean() {
if(//condition to set boolean true){
return true;
}
//if condition isn't met for 3 seconds
return false;
}
我已经考虑了一个小时了。在我问之前研究过 SO。
最佳答案
记住你的 3 秒周期开始的时刻:
private volatile long start;
并让方法检查耗时:
private boolean timedBoolean() {
if (conditionMet()) {
start = System.nanoTime();
return true;
}
return System.nanoTime()-start < TimeUnit.SECONDS.toNanos(3);
}
关于java - 满足条件时使 boolean 值 true 持续 x 秒,然后将其恢复为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20346488/