码迷,mamicode.com
首页 > 其他好文 > 详细

ObservableTest

时间:2018-12-13 14:25:55      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:format   down   atd   cat   activex   run   test   read   rom   

package com.test.rxjava;

import java.time.Duration;
import java.time.Instant;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;

import org.apache.commons.lang3.time.DurationFormatUtils;

import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

public class ObservableTest {
  
  public static void main(String[] args) {
    ObservableTest test = new ObservableTest();
    CountDownLatch latch = new CountDownLatch(1);
    test.run();
    try {
      latch.await();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private void run() {
    LinkedList<Integer> aList = new LinkedList<Integer>();
    for (int i = 0; i < 10000000; i++) {
      aList.add(i);
    }
    Instant start = Instant.now();
    Observable
    //Flowable
    //.fromIterable(aList)
    .create((ObservableOnSubscribe<Integer>) observableEmitter -> {
      //observableEmitter: 发射器
      Integer i = 0;
      while ( true){
          i++;
          System.out.println(i);
          observableEmitter.onNext(i);
      }
  })
    .observeOn(Schedulers.io())
    .subscribeOn(Schedulers.newThread())
    //.filter(i -> i%2==0)
    .subscribe(this::next, throwable -> throwable.printStackTrace(),()-> System.out.println(DurationFormatUtils.formatDurationWords(Duration.between(start, Instant.now()).toMillis(), true, true)));
  }
  private void next(Integer i) {
    try {
      Thread.sleep(100000);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+":"+i);
  }
}

 

ObservableTest

标签:format   down   atd   cat   activex   run   test   read   rom   

原文地址:https://www.cnblogs.com/tonggc1668/p/10113641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!