我在我的 sbt 项目中使用调度库。当我初始化三个未来并运行它们时,它运行良好但是我再增加一个未来然后它进入一个循环。
我的代码:
//Initializing Futures
def sequenceOfFutures() ={
var pageNumber: Int = 1
var list ={Seq(Future{})}
for (pageNumber <- 1 to 4) {
list ++= {
Seq(
Future {
str= getRequestFunction(pageNumber);
GlobalObjects.sleep(Random.nextInt(1500));
}
)
}
}
Future.sequence(list)
}
Await.result(sequenceOfFutures, Duration.Inf)
然后 getRequestionFunction(pageNumber) 代码:
def getRequestionFunction(pageNumber)={
val h=Http("scala.org", as_str)
while(h.isComplete){
Thread,sleep(1500);
}
}
我根据如何为期货配置微调线程池中的一个建议进行了尝试?
我将此添加到我的代码中:
import java.util.concurrent.Executors
import scala.concurrent._
implicit val ec = new ExecutionContext {
val threadPool = Executors.newFixedThreadPool(1000);
def execute(runnable: Runnable) {
threadPool.submit(runnable)
}
def reportFailure(t: Throwable) {}
}// Still didn't work
因此,当我使用超过四个 Futures 时,它会永远等待。有什么解决方案可以解决吗?
但它没有用有人可以建议如何解决这个问题吗?