慕姐829404
代码用kotlin写的,写android的人不管会不会写,大概应该能看懂纯kotlin工程,非android工程,线程随便用的,android里根据你的线程不同自己考虑用不用主线程packagecom.github.caijinglong.rxjavaimportio.reactivex.Flowableimportio.reactivex.FlowableSubscriberimportio.reactivex.schedulers.Schedulersimportorg.reactivestreams.Subscriptionfunmain(args:Array){varmSubscription:Subscription?=nullvallist=arrayListOf(1,2,3,4,5)Flowable.fromIterable(list).observeOn(Schedulers.io()).subscribeOn(Schedulers.computation()).subscribe(object:FlowableSubscriber{overridefunonComplete(){}overridefunonSubscribe(s:Subscription){mSubscription=ss.request(1)}overridefunonNext(p0:Int?){println("${Date().toLocaleString()}:$p0")Thread.sleep(1000)mSubscription?.request(1)}overridefunonError(p0:Throwable?){}})Thread.sleep(5000)//为了防止退出main函数结束,实际的android工程中不会退出,这里可以不用写}日志2018-1-1515:53:38:12018-1-1515:53:39:22018-1-1515:53:40:32018-1-1515:53:41:42018-1-1515:53:42:5Processfinishedwithexitcode0