【发布时间】:2018-03-28 12:51:23
【问题描述】:
首先,我是 Scala 新手。我有以下(工作)Scala 方法,必须等待长达 15 秒才能完成记录。通常,7 秒后,记录就完成了。所以,我放了一个最多 15 秒的迭代器,如果记录完成它就会停止(我不知道如何停止迭代,除了 drop 之外)。如果没有完成,它应该休眠 1 秒。问题是我必须将方法 checkIfRecordIsComplete 作为参数给出,因为我可能会将此等待方法与其他布尔函数一起使用。我怎样才能使用等待方法......让我们说checkIfOtherIsComplete?谢谢!
def checkIfRecordIsComplete(record: Record) = {
println("record.state="+record.state)
if(record.state.contains(Constants.RecordStatusComplete))
true
else
false
}
def checkIfOtherIsComplete(other: Other) = {
println("other.state="+other.state)
if(other.state.contains(Constants.OtherStatusComplete))
true
else
false
}
def wait(recordId: RecordId, maxW: Int): Unit = {
val it = Iterator.iterate(1){_+1}.take(maxW)
while(it.hasNext) {
val recordList = getRecord(recordId)
recordList.records.foreach {
record => {
if(!checkIfRecordIsComplete(record)){
Thread.sleep(1000)
it.next()
}
else
it.drop(maxW)
}
}
}
}
........................
wait(recordId, 15)
【问题讨论】:
标签: scala while-loop iterator