Guest User

Untitled

a guest
Jul 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. def main(args: Array[String]): Unit = {
  2. implicit val system: ActorSystem = ActorSystem("scratch")
  3. implicit val materializer: ActorMaterializer = ActorMaterializer()
  4. implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  5.  
  6. val start = Instant.now()
  7. def elapsed = time.Duration.between(start, Instant.now()).toMillis
  8. val intSource = Source.queue[Int](2, OverflowStrategy.dropHead)
  9. val intSink = Sink foreach { ii: Int =>
  10. Thread.sleep(1000)
  11. println(s"processing $ii at $elapsed")
  12. }
  13. val intChannel = intSource.to(intSink).run()
  14. (1 to 4) map { ii =>
  15. println(s"offer invocation for $ii at $elapsed")
  16. (ii, intChannel.offer(ii))
  17. } foreach { intFutureOfferResultPair =>
  18. val (ii, futureOfferResult) = intFutureOfferResultPair
  19. futureOfferResult onComplete { offerResult =>
  20. println(s"offer result for $ii: $offerResult at $elapsed")
  21. }
  22. }
  23. intChannel.complete()
  24.  
  25. intChannel.watchCompletion.onComplete { _ => system.terminate() }
  26. }
  27.  
  28. offer invocation for 1 at 72
  29. offer invocation for 2 at 77
  30. offer invocation for 3 at 77
  31. offer invocation for 4 at 77
  32. offer result for 1: Success(Enqueued) at 90
  33. processing 1 at 1084
  34. offer result for 2: Success(Enqueued) at 1084
  35. processing 2 at 2084
  36. offer result for 3: Success(Enqueued) at 2084
  37. processing 3 at 3084
  38. offer result for 4: Success(Enqueued) at 3084
  39. processing 4 at 4084
Add Comment
Please, Sign In to add comment