Reputation: 1033
Ok, so I am incredibly new to Scala (started yesterday). I have been reading the documents on concurrency and failed to find how to run larger tasks with Callables in a fork Join Pool. This is what I have as a sketch of what I would use in Java in Scala.
private Object fjp{
val fjp:ForkJoinPool=new ForkJoinPool(Runtime.getRuntime.availableProcessors()*2)
var w:Int=0
def invokeObjects(collection:Collection[Callable[Map[String:Int]]]){
var futures=fjp.invokeAll(collection)
w=0
while(fjp.isQuiescent()==false && fjp.getActiveThreadCount()==0){
w=w+1
}
println("Checked "+w+" times")
for(i<-0 to futures.size()){
var mp=futures.get(i).get()
//add keys to a common list
//submit count with frequency to sparse matrix
//avoid a ton of locking
}
}
}
How would I turn the code into a forkjoin pool that I can continually call. If possible, could I use a foreach without another List to get the results? Thank you for the help. It will point me in the right direction with Scala as well.
Upvotes: 0
Views: 7275
Reputation: 5999
In general, I would not bother to follow exactly that path. Scala has a very clean paradigm in order to run parallel computations that just feels more idiomatic.
If you are new to Async computation in Scala, I would recommend you to start reading this.
In particular, you can define and/or reuse several kinds of ExecutorContext in order to get the kind of threadpool you need. Or you can use the default one if you are not blocking the threads (it has one per core only, by default)
Upvotes: 2