这里有个简单的问题,我只需要在经过特定时间段(以毫秒为单位)后才执行某种方法:
static double incrTrueBinaryErr = 62.5;
static double incrTrueBinaryMov = 31.25;
static double incr01001 = 360000;
static double incr_1101 = 125;
static double incr05008 = 3600000;
static double incr10004 = 1024000;
static double incr22000 = 1000;
static double incr22001 = 1000;
Date currDate;
while (true)
{
currDate = new Date();
if (currDate.getTime() == currDate.getTime() + incrTrueBinaryErr)
//do stuff
if (currDate.getTime() == currDate.getTime() + incrTrueBinaryMov)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr01001)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr_1101)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr05008)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr10004)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr22000)
//do stuff
if (currDate.getTime() == currDate.getTime() + incr22001)
//do stuff
}
但是,由于时间间隔的具体程度,我的程序从不执行任何操作。
这样做的最佳方法是什么?
最佳答案
仅当currDate.getTime() == currDate.getTime() + incr01001
时,这种表达式true
的值才为incr01001 == 0
。出于相同的原因,仅当x == x + y
时使用y == 0
。