Skip to content

Instantly share code, notes, and snippets.

@rssh
Last active April 11, 2016 06:57
Show Gist options
  • Save rssh/79a2cae103868241e1f788955fbaadb2 to your computer and use it in GitHub Desktop.
Save rssh/79a2cae103868241e1f788955fbaadb2 to your computer and use it in GitHub Desktop.
Full example to Future behaviour changed in dependency from execution context 'why scala is not my ideal language' presentation on #scalaua 2016
package x
import scala.concurrent._
import scala.concurrent.duration._
import java.util.concurrent.{Future=>JFuture,_}
object Implicits
{
implicit val ec: ExecutionContext = {
// with singetheaded - timeout
//ExecutionContext.fromExecutor(Executors.newSingleThreadExecutor())
// with global - ok
scala.concurrent.ExecutionContext.Implicits.global
}
}
import Implicits._
object XService
{
def retrieve(legend:String):Future[Int] =
Future{
System.out.println(s"start $legend")
Thread.sleep(1000)
val retval = Await.result(Future{
Thread.sleep(1000)
10
}, 1 minute)
System.out.println(s"end $legend")
retval
}
}
object Test
{
def main(args: Array[String]):Unit =
{
val zf = for(x <- XService.retrieve("x");
y <- XService.retrieve("y")) yield x+y
val z = Await.result(zf, 1 minute)
System.err.println("result:"+z)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment