Last active
December 21, 2015 01:39
-
-
Save lshoo/6229398 to your computer and use it in GitHub Desktop.
Future的onComplete后面的内容输出如何显示?
原因主线程结束时,future线程还没执行完
增加Thread.sleep(5000)
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
package neophyte.guide | |
import concurrent._ | |
import ExecutionContext.Implicits.global | |
import neophyte.guide.PromiseFutures.TaxCut | |
import scala.util.{Failure, Success} | |
object PromiseFutures { | |
case class TaxCut(reduction: Int) | |
def main(args: Array[String]): Unit = { | |
val taxCutF: Future[TaxCut] = Government.redeemCampaignPledge() | |
println("Now that they're elected, let's see if they remember their promise...") | |
taxCutF.onComplete { | |
case Success(TaxCut(reduction)) => println(s"A miracle! They really cut our taxes by $reduction percentage points!") | |
case Failure(ex) => println(s"They broke their promises! Again! Because of a ${ex.getMessage}") | |
} | |
Thread.sleep(5000) | |
} | |
} | |
object Government { | |
def redeemCampaignPledge(): Future[TaxCut] = { | |
val p = Promise[TaxCut]() | |
future { | |
println("Starting the new legislative period") | |
Thread.sleep(2000) | |
p.success(TaxCut(20)) | |
println("We reduced the taxes! You must reelect us!!!!1111") | |
} | |
p.future | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment