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. ROS Learning-025 (提高篇-003 A Mobile Base-01) 控制移动平台

    ROS 提高篇 A Mobile Base-01 - 控制移动平台 - 基本知识 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 ...

  2. JavaWeb_泛型(Generic)

    JDK5以前,对象保存到集合中就会失去其特性,取出时通常要程序员手工进行类型的强制转换,这样不可避免的就会引发程序的一些安全性问题.例如: ArrayList list = new ArrayList ...

  3. Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)

    Linux内核源码分析-链表代码分析 分析人:余旭 分析时间:2005年11月17日星期四 11:40:10 AM 雨 温度:10-11度 编号:1-4 类别:准备工作 Email:yuxu97101 ...

  4. 多线程学习-基础(一)Thread和Runnable实现多线程

    很久没记录一些技术学习过程了,这周周五的时候偶尔打开“博客园”,忽然让我产生一种重拾记录学习过程的想法,记录下学习研究过程的一点一滴,我相信,慢慢地就进步了!最近想学习一下多线程高并发,但是多线程在实 ...

  5. 小小c#算法题 - 7 - 堆排序 (Heap Sort)

    在讨论堆排序之前,我们先来讨论一下另外一种排序算法——插入排序.插入排序的逻辑相当简单,先遍历一遍数组找到最小值,然后将这个最小值跟第一个元素交换.然后遍历第一个元素之后的n-1个元素,得到这n-1个 ...

  6. Redis集群Windows

    一 所需软件:Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb 二 安装配置redis redis下载地址   ...

  7. C#中如何防止Excel做科学计算法转换

    C#中如何防止Excel做科学计算法转换  string style = @"<style>.text{mso-number-format:\@;}</style>& ...

  8. vs code进行c/c++开发

    vs code是微软公司开发的跨平台编辑器,丰富的插件功能可以满足各种编程语言的编码,编译和调试.由于vs code本身只是一个编辑器,所以你需要准备编译工具链.本文针对的是windows系统,我这里 ...

  9. 删除XML文档中某节点

    前几天Insus.NET在写了一系列XML文档进行操作.创建 <怎样创建XML文档> http://www.cnblogs.com/insus/p/3276944.html       & ...

  10. [Spring Boot]Request method 'GET' not supported

    在网上查了些资料没有解决,检查代码发现 @PostMapping public String add(Employee employee){ System.out.println(employee); ...