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);
}
}
package com.test.rxjava;

import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch; import org.apache.commons.lang3.time.DurationFormatUtils; import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers; public class ObservableTest implements Iterable<Integer> { private Integer begin;
private Integer end; private ObservableTest(Integer begin, Integer end) {
this.begin = begin;
this.end = end;
} public static void main(String[] args) {
ObservableTest test = new ObservableTest(0, 1_000_000);
CountDownLatch latch = new CountDownLatch(1);
test.run(test, latch);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
} private void run(ObservableTest test, CountDownLatch latch) {
Instant start = Instant.now();
//Observable
Flowable
.fromIterable(test).observeOn(Schedulers.io()).subscribeOn(Schedulers.newThread())
.subscribe(this::handleNext, e -> {
e.printStackTrace();
latch.countDown();
}, () -> {
System.out.println(DurationFormatUtils
.formatDurationWords(Duration.between(start, Instant.now()).toMillis(), true, true));
latch.countDown();
});
} private void handleNext(Integer i) {
System.out.println("consumer: "+Thread.currentThread().getName() + ":" + i);
} public Iterator<Integer> iterator() {
return new Itr(begin, end);
} private class Itr implements Iterator<Integer> {
private Integer begin;
private Integer end; private Itr(Integer begin, Integer end) {
this.begin = begin;
this.end = end;
} @Override
public boolean hasNext() {
return begin < end;
} @Override
public Integer next() {
begin++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("producer: "+Thread.currentThread().getName() + ":" + begin);
return begin;
}
}
}
package com.test.rxjava;

import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch; import org.apache.commons.lang3.time.DurationFormatUtils; import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers; public class ObservableTest implements Iterable<Integer> { private Integer begin;
private Integer end; private ObservableTest(Integer begin, Integer end) {
this.begin = begin;
this.end = end;
} public static void main(String[] args) {
ObservableTest test = new ObservableTest(0, 100_000_000);
CountDownLatch latch = new CountDownLatch(1);
test.run(test, latch);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
} private void run(ObservableTest test, CountDownLatch latch) {
Instant start = Instant.now();
Observable
.fromIterable(test)
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.newThread())
.filter(i -> i%2==0)
.buffer(1000)
.subscribe(this::handleNext, e -> {
e.printStackTrace();
latch.countDown();
}, () -> {
System.out.println("Observable:" + DurationFormatUtils
.formatDurationWords(Duration.between(start, Instant.now()).toMillis(), true, true));
latch.countDown();
});
} private void handleNext(List<Integer> i) {
System.out.println("consumer: "+Thread.currentThread().getName() + ":" + i.get(i.size()-1));
} public Iterator<Integer> iterator() {
return new Itr(begin, end);
} private class Itr implements Iterator<Integer> {
private Integer begin;
private Integer end; private Itr(Integer begin, Integer end) {
this.begin = begin;
this.end = end;
} @Override
public boolean hasNext() {
return begin < end;
} @Override
public Integer next() {
begin++;
//System.out.println("producer: "+Thread.currentThread().getName() + ":" + begin);
return begin;
}
}
}
package com.test.rxjava;

import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch; import org.apache.commons.lang3.time.DurationFormatUtils; import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers; public class FlowableTest implements Iterable<Integer> { private Integer begin;
private Integer end; private FlowableTest(Integer begin, Integer end) {
this.begin = begin;
this.end = end;
} public static void main(String[] args) {
FlowableTest test = new FlowableTest(0, 100_000_000);
CountDownLatch latch = new CountDownLatch(1);
test.run(test, latch);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
} private void run(FlowableTest test, CountDownLatch latch) {
Instant start = Instant.now();
Flowable
.fromIterable(test)
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.newThread())
.filter(i -> i%2==0)
.buffer(1000)
.subscribe(this::handleNext, e -> {
e.printStackTrace();
latch.countDown();
}, () -> {
System.out.println("Flowable:" + DurationFormatUtils
.formatDurationWords(Duration.between(start, Instant.now()).toMillis(), true, true));
latch.countDown();
});
} private void handleNext(List<Integer> i) {
System.out.println("consumer: "+Thread.currentThread().getName() + ":" + i.get(i.size()-1));
} public Iterator<Integer> iterator() {
return new Itr(begin, end);
} private class Itr implements Iterator<Integer> {
private Integer begin;
private Integer end; private Itr(Integer begin, Integer end) {
this.begin = begin;
this.end = end;
} @Override
public boolean hasNext() {
return begin < end;
} @Override
public Integer next() {
begin++;
//System.out.println("producer: "+Thread.currentThread().getName() + ":" + begin);
return begin;
}
}
}

去掉

.subscribeOn(Schedulers.newThread())

Observable Flowable Test的更多相关文章

  1. Retrofit 简介 wiki 文档

    简介 Type-safe HTTP client for Android and Java by Square, Inc. GitHub主页:https://github.com/square/ret ...

  2. 实战SpringCloud响应式微服务系列教程(第三章)

    接着之前的: 实战SpringCloud响应式微服务系列教程(第一章) 实战SpringCloud响应式微服务系列教程(第二章) 1.1.3Reactor框架 响应式编程是一种编程模型,本节将介绍这种 ...

  3. Rxjava2 介绍与详解实例

    目录 前言 RX介绍 Rx模式 Rx使用依赖: Rxjava的入门基础 1. Observable 2. Flowable 3. Single 4. Completable 5. Maybe 6. S ...

  4. Android RxJava 2 的用法 just 、from、map、subscribe、flatmap、Flowable、Function、Consumer ...【转】

    先简单说说RxJava的用途与价值 原文出处:Android RxJava 2 的用法 用途: 异步 (也就是开线程跳转) 价值: 面对复杂的逻辑,它依然 简洁 ,代码 易读 RxJava2 与 Rx ...

  5. angular2系列教程(八)In-memory web api、HTTP服务、依赖注入、Observable

    大家好,今天我们要讲是angular2的http功能模块,这个功能模块的代码不在angular2里面,需要我们另外引入: index.html <script src="lib/htt ...

  6. Angular2学习笔记——Observable

    Reactive Extensions for Javascript 诞生于几年前,随着angular2正式版的发布,它将会被更多开发者所认知.RxJs提供的核心是Observable对象,它是一个使 ...

  7. Rxjava cold/hot Observable

    create Observable分为cold以及hot两种,cold主要是静态的,每次subscribe都是从头开始互不干扰,而hot的在同一时刻获得的值是一致的 cold Observable 使 ...

  8. 设计模式--观察者模式初探和java Observable模式

    初步认识观察者模式 观察者模式又称为发布/订阅(Publish/Subscribe)模式,因此我们可以用报纸期刊的订阅来形象的说明: 报社方负责出版报纸. 你订阅了该报社的报纸,那么只要报社发布了新报 ...

  9. 观察者模式(Observer和Observable实现)

    package com.wzy.java8.thread; import java.util.Observable; import java.util.Observer; public class D ...

随机推荐

  1. HTTP、TCP、UDP、Socket关系详解

    TCP.UDP和HTTP关系是什么? 1.TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议.在传输层中有TCP ...

  2. cocos2d-js 序列帧动画

    var spriteCache = cc.spriteFrameCache;spriteCache.addSpriteFrames(res.fireworks_plist,res.fireworks_ ...

  3. Win10_禁用自动更新(官方版)

    1> win键>输入服务>打开>找到windowsUpdate-->启动类型为-禁用 -->> 恢复失三个选项改为-->>无操作 2>win ...

  4. 机器人自主移动的秘密:实际应用中,SLAM究竟是如何实现的?(二)

    博客转载自:https://www.leiphone.com/news/201612/FRzmoEI8Iud6CmT2.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...

  5. Elasticsearch suggester搜索建议初步

    环境 Elasticsearch 2.3.5 Elasticsearch-ik-plugin 实现 搜索建议的对象 假设有以下两个json对象,需要对其中tags字段进行搜索建议: //对象Produ ...

  6. Java 分析模板方法设计模型

    http://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html //父类 abstract class Operate{ prot ...

  7. Windows7 安装TensorFlow(本人试了好多方法后的成果)

    本人机器为64位win7 首先安装python,版本一定要注意,TennsorFlow要使用 Python3.0 系列版本不能使用2.0系列版本,但是TensorFlow 的安装包目前windows版 ...

  8. [转]delphi xe6 android屏幕操持常亮

    1) setting the Project Options, Uses Permissions, Wake lock = True 2) Adding Android.JNI.PowerManage ...

  9. execve(".. ",[".. ",".. "],[/* ..*/])第二个 参数 数组硬传

    strace.第一次跟踪点东西的启动. 第一次看见..execve(".. ",[".. ",".. "],[/* ..*/]) 第二个 参 ...

  10. FlowLayoutPanel控件控制里面子控件换行

    // 摘要: // 设置值,该值表示的流中断设置 System.Windows.Forms.FlowLayoutPanel 控件. // // 参数: // control: // 子控件. // / ...