View StackTraceDirect.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.IllegalStateException | |
at com.komanov.future.examples.ExceptionDemo$.$anonfun$stackTrace$3(ExceptionDemo.scala:14) | |
at scala.runtime.java8.JFunction1$mcII$sp.apply(JFunction1$mcII$sp.java:23) | |
at scala.util.Success.$anonfun$map$1(Try.scala:255) | |
at scala.util.Success.map(Try.scala:213) | |
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292) | |
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33) | |
at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33) | |
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64) | |
at com.komanov.future.package$DirectExecutor.execute(package.scala:29) |
View StackTraceGlobal.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.IllegalStateException | |
at com.komanov.future.examples.ExceptionDemo$.$anonfun$stackTrace$3(ExceptionDemo.scala:14) | |
at scala.runtime.java8.JFunction1$mcII$sp.apply(JFunction1$mcII$sp.java:23) | |
at scala.util.Success.$anonfun$map$1(Try.scala:255) | |
at scala.util.Success.map(Try.scala:213) | |
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292) | |
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33) | |
at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33) | |
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64) | |
at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402) |
View DirectStackTraceDemo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stackTrace(implicit ec: ExecutionContext): Any = { | |
Await.result( | |
Future.successful(1) | |
.map(v => v + 1) | |
.flatMap(v => Future.successful(v)) | |
.map { v => | |
new IllegalStateException().printStackTrace() | |
v | |
}, | |
10.seconds |
View RpcClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RpcClient(ownEc: ExecutionContext, appEc: ExecutionContext) { | |
def call: Future[String] = | |
Future("a")(ownEc) | |
.map(identity)(appEc) // here we move back to an application execution context | |
} |
View DirectExecutorContextUsage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rpcCall: Future[String] | |
def daoCall(id: String): Future[Int] | |
def extractId(idStr: String): Future[String] | |
def convertDbValue(value: Int): Future[Int] | |
import directExecutionContext | |
for { | |
idStr <- rpcCall | |
id <- extractId(idStr) // executed in RPC execution context |
View MapDescribed.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def map[U](f: T => U) | |
(implicit ec: ExecutionContext): Future[U] = { | |
val promise = Promise() | |
if (isCompleted) // in real code this is an atomic operation + pattern matching | |
// even if it's completed, callback is submitted to ExecutionContext | |
ec.execute(() => { | |
f(result) | |
}) | |
else | |
registerCallback(f)(ec) |
View SmartMap.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def smartMap[U](f: T => U)(implicit ec: ExecutionContext): Future[U] = | |
if (future.isCompleted) { | |
if (future.value.get.isSuccess) | |
wrap(f(future.value.get.get)) | |
else | |
future.asInstanceOf[Future[U]] | |
} else { | |
future.map(f)(ec) | |
} |
View Benchmark.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var future = Future.successful(0) | |
for (_ <- 1 to 100) { | |
future = future.map(v => v + 1) | |
} | |
require(Await.result(future, 10.seconds) == 100) |
View Transformations.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rpcCall: Future[String] = ??? | |
rpcCall | |
.map(_.split('_')) | |
.filter(_.length < 4) | |
.map { | |
case Array(single) => single | |
case Array(first, second@_) => first | |
case Array(first@_, second, third@_) => third | |
case _ => "" |
View ExecutionContext.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait ExecutionContext { | |
def execute(runnable: Runnable): Unit | |
def reportFailure(cause: Throwable): Unit | |
} |
NewerOlder