2006 • The free lunch is over • Moore’s Law still applies but only the number of cores in a single chip is increasing. • The new reality: Amdahl's Law. ref: http://en.wikipedia.org/wiki/Amdahl's_law 2 Friday, August 2, 13
an non-blocking operation on the result when it is ready. • Only blocking wait and busy wait are available. – future.get(10, TimeUnit.Minutes) – while (future.isDone() == false) { //do other works if they exists; otherwise Thread.sleep(1000) } Friday, August 2, 13
2. 5 3. } 4. 5. • create a future and blocking wait for result. def apply[T](body: 㱺 T): Future[T] Starts an asynchronous computation and returns a Future object with the result of that computation. The result becomes available once the asynchronous computation is completed. Friday, August 2, 13
Thread.sleep(5000) 3. 5 4. } 5. // throw exception or return a value. 6. val ret = Await.result(resultFuture, 5.minutes) • create a future and blocking wait for result. def result[T](awaitable: Awaitable[T], atMost: Duration): T Await and return the result (of type T) of an Awaitable. Although this method is blocking, the internal use of blocking ensures that the underlying ExecutionContext to properly detect blocking and ensure that there are no deadlocks. Friday, August 2, 13
value of this Future. If the future is not completed the returned value will be None. If the future is completed the value will beSome(Success(t)) if it contains a valid result, or Some(Failure(error)) if it contains an exception. 1. resultFuture.value match { 2. case Some(Success(value)) => // do something with value 3. case Some(Failure(exception)) => // handle exception 4. case None => // do nothing 5. } Friday, August 2, 13
is completed successfully (i.e. with a value), apply the provided partial function to the value if the partial function is defined at that value. def onFailure[U](callback: PartialFunction[Throwable, U]): Unit When this future is completed with a failure (i.e. with a throwable), apply the provided callback to the throwable. 1. val searchFuture: Future[SearchResult] = // invoke search 2. searchFuture.onSuccess { 3. case res: SearchResult => {???}// update UI with the result. 4. } 1. searchFuture.onFailure { 2. case e: SocketTimeoutException => //notify user request timeout 3. case e: ConnectException => // notify user service is 4. // unreachable 5. } Friday, August 2, 13
a new future by applying a function to the successful result of this future. If this future is completed with an exception then the new future will also contain this exception. 1. val historyFuture: Future[BrowseHistory] = // invoke user service 2. val future: Future[Model] = historyFuture.map { 3. history: BrowseHistory => { 4. // transform BrowseHistory to Model 5. } 6. } 1. val one = Future { 1 } 2. val two = one.map { i => i + 1 } // value is 2. Friday, August 2, 13
Creates a new future by applying a function to the successful result of this future, and returns the result of the function as the new future. 1. val historyFuture: Future[BrowseHistory] = // invoke user service 2. val future: Future[Recommendations] = historyFuture.flatMap { 3. history: BrowseHistory => { 4. service.findRelatedItems(history) // Future[Recommendations] 5. } 6. } Friday, August 2, 13
of this and that future, and creates a new future holding the tuple of their results. 1 val a = Future { 1 } 2 val b = Future { 2 } 3 val c = a zip b // return Future[(Int, Int)] Friday, August 2, 13
Creates a new future which holds the result of this future if it was completed successfully, or, if not, the result of the that future if that is completed successfully. If both futures are failed, the resulting future holds the throwable object of the first future. Using this method will not cause concurrent programs to become nondeterministic. 1 val failed = future.failed(new RuntimeException("Failed")) 2 val default = future.successful( 5 ) 3 val future = failed fallbackTo default 4 Await.result(future, Duration.Zero) // evaluates to 5 Friday, August 2, 13
new future that will handle any matching throwable that this future might contain. If there is no match, or if this future contains a valid result then the new future will contain the same. Error Handling future {6 / 0} recover { case e: ArithmeticException => 0 } // result: 0 future {6 / 2} recover { case e: ArithmeticException => 0 } // result: 3 1 future { /** network call */ } recover { 2 case e: SocketTimeoutException => ??? 3 case e: IOException => ??? 4 } Friday, August 2, 13
new future that will handle any matching throwable that this future might contain by assigning it a value of another future. If there is no match, or if this future contains a valid result then the new future will contain the same result. 1. val result = Future { 2. defaultDataSource.findItem(5) //returns Future[Option[Item]] 3. } recoverWith { 4. case SocketTimeoutException => backupSource.findItem(5) 5. } Error Handling Friday, August 2, 13
numbers in files. – files come from s3. Their size can be ranged from 100mb to 10gb. – I don’t want to fully read the file and then sort the file. 40 Friday, August 2, 13