This question is probably better asked on one of the Scala mailing lists. 
 You'll perhaps get a better answer.

To understand what is going on, take a look at the signatures for 
Future.apply and Future.foreach.  Each method has multiple parameter lists, 
with the last requiring (a possibly implicit) execution context.  The 
execution contexts past to these parameters will influence the behavior of 
the methods.  When you import 
scala.concurrent.ExecutionContext.Implicits.global you make the implicit 
val 'global' availble in your current scope.  Because this val is marked 
implicit, Scala will allow this val to participate in implicit resolution, 
following all the various rules for resolving implicits (better to get a 
book or ask a Scala list, since those rules are quite complicated).

So now with your Implicits.global import, your effective code looks like:

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

object ThreadAPP extends App {

  val future = Future {
    println("done with the future")
    "hello"
  }(global)

  future.foreach(println)(global)

  Thread.sleep(500)

}

So now it should be more obvious that your Future {} logic will be executed 
following whatever behavior global defines and the foreach behavior for 
how/when the foreach closure is called when the future completes is also 
determined by whatever the nature of global is.

So what's the nature of global?  The ScalaDocs don't give specifics.  A 
Scala mailing list might be able to clarify, and it might be documented 
somewhere within the language spec.  However, I think it's likely that this 
particular behavior simply isn't documented and could be subject to change. 
 A super quick demo like you've put together clearly demonstrates that 
these must be daemon threads behind the scenes, because they obviously 
don't prevent the termination of the VM.

Hopefully this removes some of the mystery and makes things more clear.

Your main thread is your main thread.  Each of your two Scala Future calls 
explicitly require an execution context, that you happen to resolve 
implicitly.  Your main thread is delegating work to an external execution 
context (global) and this appears to be using a thread pool backed by 
daemon threads.

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to