We have code that should work faster. It is already profiled, so we would like to use multiple threads. I usually set up a queue in memory and had a number of threads occupying the queue jobs and calculating the results. For general data, I would use ConcurrentHashMap or similar.
I do not want to go this route again. From what I read using actors, you get a cleaner code, and if I use akka migrating more than 1 jvm, it should be easier. It's true?
However, I don’t know how to think in actors, so I’m not sure where to start.
To better understand the problem, here is a sample code:
case class Trade(price:Double, volume:Int, stock:String) {
def value(priceCalculator:PriceCalculator) =
(priceCalculator.priceFor(stock)-> price)*volume
}
class PriceCalculator {
def priceFor(stock:String) = {
Thread.sleep(20)
50.0
}
}
object ValueTrades {
def valueAll(trades:List[Trade],
priceCalculator:PriceCalculator):List[(Trade,Double)] = {
trades.map { trade => (trade,trade.value(priceCalculator)) }
}
def main(args:Array[String]) {
val trades = List(
Trade(30.5, 10, "Foo"),
Trade(30.5, 20, "Foo")
)
val priceCalculator = new PriceCalculator
val values = valueAll(trades, priceCalculator)
}
}
, - , .