I'm spawning a large number (~100.000) http-request tasks as Futures in Scala. This takes a while, so I would like to be able to keep track of how many of those futures have successfully completed or failed by incrementing a global counter. I want, however, to avoid race conditions. Are there options in Scala to make an atomic counter? Or is there better way to go?
The code looks like this:
val futures = for(i <- 0 until nSteps) yield future {
...
val content = blocking { ... http request ... }
process(content)
}
Await.result(Future.sequence(futures),2 hours)
I would suggest using the standard Java
AtomicInteger
. You can increment it using theincrementAndGet()
method, and obtain the current value via itsget()
method.