What’s the the connection between those Future-related class and traits in Scala, and why are they sprinkled over different packages?
I have found those:
abstract class scala.actors.Future
object scala.actors.Futures
trait/object scala.collection.parallel.FutureThreadPoolTasks
trait scala.concurrent.FutureTaskRunner
trait scala.parallel.Future (the package consists of only this file...)
Do they significantly different things or is there another reason why they can’t be consolidated?
Is there a good example showing when one would use the one thing or the other?
Edit: Bounty for explaining what each of the classes/traits/objects does and how they justify their existance/how they are useful.
scala.actors._abstract class FutureFirst of all, lets see what the documentation says:
And that is basically all there is. If you are communicating with an Actor from anywhere outside of another actor (which can receive asynchronous replies to messages simply with another message, using the
senderreference) and you need the reply to a sent message, you have two choices:So a Future is a placeholer for a value which does not yet exist, but probably will in the near future. The following is also interesting:
This allows you to do whatever you want until the value you need has been computed/fetched, and you can periodically check if the value has become available.
When digging a bit into the Scala library source code, I found out that Futures are actually just actors.
Futureitself is a anabstract class, which is extended by theprivate class FutureActor. This last class is the one that actually implements theFuture-functionality.object FuturesThe
object Futuresis by far not as interesting, as it is merely a container for the “Methods that operate on futures”, the handy factory methodfuturewhich asynchronously evaluates the passed block, returning a future representing the result. A small example would be this:Which should result in something like
This demonstrates that the
future-call does not block the following code until you actually try to retrieve the value of the future (note that the output is non-deterministic.After futurecould also appear at the beginning of the output).scala.collection.parallelThis packages is new to Scala 2.9.x and implements parallel counterparts for some of our favorite collections. These all start with
Par:As you may have known or guessed already, these collections implement all possible operations in a parallel manner without you having to worry about it. A small demonstration:
The result will always be the same, but the order in which the elements are processed is again non-deterministic. Also, if you are on a multicore system, you will probably experience a nice performance boost for larger collections.
trait FutureThreadPoolTasksThe
FutureThreadPoolTaskstrait extends theTaskstrait, so lets take a look at that one first. The comment above the trait says:Judging from the other source comments and the methods found in the
Taskstrait, a Task represents a unit of work which needs to be computed. Depending on wether or not a problem is divisible further and if there are more resources available, a Task can split up a Task further by creating more tasks.Now the
FutureThreadPoolTaskstrait itself is just a way to compute tasks, which uses thejava.util.concurrent.Futureclass for its synchronization, that is, it does not usescala.actors.Future! From the source:object FutureThreadPoolTasksOnce again not very spectacular, just a companion object containing a few (actually only three) utility methods which the
FutureThreadPoolTaskstrait uses.scala.concurrentThe documentation on these classes is really bad and apparently there are very few if any (I didn’t find a single one) examples which demonstrate the usage of these classes. I will definitely try to gather more information on these and expand on this section as soon as I can!
scala.paralleltrait FutureThis seems to be a “Work in progess”, as the
scala.parallelpackage only contains this trait. From what I can tell, this is going to be related to aFutureimplementation which does not useActors, but that is just a guess. The signature of the trait is the followingI am not even going to try to explain the @specialized annotation or variances (the + before the generic R type), but the basic idea in this trait is that a Future is a function which, when executed, returns the value (and must therefor block if it has not been computed yet).
Also, there are only two methods inside the trait itself,
applyandisDone. My guess is thatisDone, just like thescala.actors.Future.isSet, is supposed to be a non-blocking call to see if the value has been computed, and theapplymethod should be used to actually retrieve the value.