-- 反应式编程 异步非阻塞 scala优化 生产中消费者
Array(11,22,33)
.map(function1) // map1
.map(function2) // map2
需要map1执行完毕再执行map2
for循环 则顺序是11的map1 11的map2 22的map1 22的map2
map1不断执行 map2也不断执行. 这样map2就减少了等待时间. 注意这里使用scala反应式编程,不是简单的生产者消费者队列.
- package com.dahua88.monixLearn
-
- import monix.eval._
-
- import scala.concurrent.duration.DurationInt
- import monix.reactive._
- import monix.execution.Scheduler.Implicits.global
-
- import java.lang.Thread.sleep
- import scala.util.Random
-
- // Needed below
- import scala.concurrent.duration._
-
- object MonixLearn1 {
- def main(args: Array[String]): Unit = {
-
-
- // Nothing happens here, as observable is lazily
- // evaluated only when the subscription happens!
- val tick = {
- Observable
- .fromIterable[Int](11 to 2200000 toIterable)
- .as