Reactive Programming with RxJava-Chapter1:Reactive Programming with RxJava

RxJava is a specific implement of reactive programming for Java and Android that is influenced by functional programming.It favors function composition,avoidance of global state and side effects,and thinking in streams to compose asynchronous and event-based programs.It begins with the observer pattern of producer/consumer callbacks and extends it with dozens of operators that composing,transforming,scheduing,throtting,error handing,and lifecycle management.

Reactive Programming and RxJava

Reactive programming is a general programming term that is focused on reacting to changes,such as data values or events.It can and often is done imperatively.

The short answer to what reactive-functional programming is solving is concurency and parallelism.More colloquially,it is solving callback hell,which results from addressing reactive and asynchronous use cases in an imperative way.

When You Need Reactive Programming

  • Processing user events such as mouse movement and clicks,keyboard typing.
  • Responding to and processing any and all latency-bound IO events from disk or network ,given that IO is inherently asynchronous(a request is made,time passes,a response might or might not be received,which then triggers futher work).
  • Handing events or data pushed at an application by a producer it cannot control.

How RxJava Works

1、Push versus Pull

2、Async versus Sync

A synchronous Observable would be subsrcribed to,emit all data using the subscriber`s thread,and complete(if finite).

The actual criteria that is generally important is whether the Observable event prodiction is blocking of nonblocking,not whether it is synchronous or asynchronous.

Reason:

  • concurrency can come from multiple places,not just threadpools.
  • In-memory data
  • Synchronous computation(such as operators)

3、Concurrency and Parallelism

A single Observable stream must always be serialized and thread-safe (serialize())
serialize()

  • onNext() is meant for us humans to use,and concurrency is difficult.
  • Some operations just aren`t possible with concurrent emission
  • Performance is affected by synchronization overhead because all observers and operators would need to be thread-safe,even if most of the time data arrives sequentially.
  • It`s often slower to do generic fine-grained parallelism.And a stream does not know the work ahead of time,it just receives data via onNext() and therefore cannot automatically chunk the work.

4、Lazy versus Eager

  • Subscription,not construction starts work.
  • Observables can be reused.

5、Duality

The Rx Observable allows programming with async data via push just like Streams around Iterables and Lists using synchronous pull

6、Cardinality

  • Event stream
  • Multiple values
    • Items can be processed as receiver rather tthan waiting for the entire collection to arrive
    • It keeps memory usage limited to that needed per item rather than needing to allocate and collect memory for the entire collection.
  • Composition
  • Single,instead of Observable to represent a “stream of one” simplifies consumption because a developer must consider only the following behaviors for the Single type:
    • It can respond with an error
    • Never respond
    • Respond with a success
  • Completable
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值