反应式编程优点/缺点 [英] Reactive Programming Advantages/Disadvantages

查看:614
本文介绍了反应式编程优点/缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究并尝试使用Reactor和RxJava进行编码的Reactive Style。我确实理解,与单线程执行相比,反应式编码可以更好地利用CPU。

I keep studying and trying Reactive Style of coding using Reactor and RxJava. I do understand that reactive coding makes better utilization of CPU compared to single threaded execution.

在基于Web的应用程序中,反应式编程与命令式编程之间是否有任何具体的比较?

Is there any concrete comparison between reactive programming vs imperative programming in web based applications?

通过对非反应式编程使用反应式编程实现的性能增益,吞吐量是多少?

How much is the performance gain, throughput I achieve by using reactive programming over non-reactive programming?

还原反应编程有哪些优点和缺点?

Also what are the advantages and disadvantages of Reactive Programming?

有没有统计基准?

推荐答案

好吧,反应式编程意味着您正在异步执行所有IO绑定任务,例如网络调用。例如,您的应用程序调用外部REST API或数据库,您可以异步执行该调用。如果这样做,您当前的线程不会阻止。您只需生成一个或几个线程就可以提供大量请求。如果您遵循阻塞方法,则需要有一个线程来处理每个请求。您可以参考我的多部分博客文章第一部分第二部分第三部分了解更多详情。

Well, Reactive Programming means you are doing all your IO bound tasks such as network calls asynchronously. For an instance say your application calls an external REST API or a database, you can do that invocation asynchronously. If you do so your current thread does not block. You can serve lots of requests by merely spawning one or few threads. If you follow blocking approach you need to have one thread to handle each and every request. You may refer my multi part blog post part one, part two and part three for further details.

除此之外,你可以使用回调来做同样的事情。您可以使用回调进行异步调用。但是,如果你这样做,有时你可能最终会回调地狱。在另一个内部进行一次回调会导致非常复杂的代码,这些代码很难维护。另一方面,RxJava允许您编写非常简单,可组合和可读的异步代码。此外,RxJava为您提供了许多强大的运算符,例如Map,Zip等,这使得您的代码更加简单,同时由于并行执行不依赖于彼此的不同任务而提高了性能。

Other than that you may use callbacks to do the same. You can do asynchronous invocation using callbacks. But if you do so sometimes you may ended up with callback hell. Having one callback inside another leads to very complex codes which are very hard to maintain. On the other hand RxJava lends you write asynchronous code which is much more simple, composable and readable. Also RxJava provides you a lots of powerful operators such as Map, Zip etc which makes your code much more simple while boosting the performance due to parallel executions of different tasks which are not dependent on each other.

RxJava不是另一个带有运算符集的Observer实现,而是为您提供了非常方便的错误处理和重试机制。

RxJava is not another Observer implementation with set of operators rather it gives you good error handling and retry mechanisms which are really handy.

但是我没有使用命令式编程方法对RxJava进行任何基准测试,以便在统计上对您进行推荐。但我非常确定RxJava应该比阻塞机制产生更好的性能。

But I have not conducted any bench marking of RxJava with imperative programming approach to commend you statistically. But I am pretty much sure RxJava should yield good performance over blocking mechanisms.

更新

由于我随着时间的推移积累了更多的经验,我想在我的答案中添加更多的观点。

Since I gathered more experience over time, I thought of adding more points to my answer.

根据文章,ReactiveX是一个用于通过使用可观察序列来编写异步和基于事件的程序的库。我想你首先要阅读这篇介绍性文章。

Based on the article, ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences. I reckon you to go through this introductory article in the first place.

这些是被动系统的一些属性:事件驱动,可扩展,弹性,响应

These are some properties of reactive systems: Event Driven, Scalable, Resilient, Responsive

对于RxJava,它为程序员提供了两个主要功能。首先,它使用丰富的运算符(如zip,concat,map等)提供了一个很好的可组合API。这样可以生成更简单易读的代码。在代码方面,可读性和简单性是最重要的属性。其次,它提供了出色的抽象,使并发成为声明式。

When it comes to RxJava it offers two main facilities to a programmer. First it offers a nice composable API using a rich set of operators such as zip, concat, map etc. This yields more simple and readable code. When it comes to code, readability and simplicity are the uttermost important properties. Second, it provides excellent abstractions, that enable concurrency to become declarative.

一个流行的误解是默认情况下Rx是多线程的。说实话,Rx默认是单线程的。如果你想异步做事,那么你必须通过传递相关的调度程序,使用 subscribeOn observeOn 运算符明确告诉它。 RxJava为您提供线程池来执行异步任务。有许多调度程序,如IO,Computation等。顾名思义,IO调度程序最适合IO密集型任务,例如网络调用等。相反,计算调度程序适用于更多CPU密集型计算任务。您也可以使用RxJava连接自己的Executor服务。内置的调度程序主要帮助您摆脱维护自己的Executor服务,使您的代码更简单。

A popular misconception is that Rx is multithreaded by default. To be honest, Rx is single-threaded by default. If you want to do things asynchronously, then you have to tell it explicitly using subscribeOn and observeOn operators by passing relevant schedulers. RxJava gives you thread pools to do asynchronous tasks. There are many schedulers such as IO, Computation and so forth. IO scheduler as the name suggests is best suited for IO intensive tasks such as network calls etc. In contrary Computation scheduler is good for more CPU intensive computation tasks. You can also hook up your own Executor services with RxJava too. The built in schedulers mainly helps you to get rid of maintaining your own Executor services, making your code more simple.

最后关于subscribeOn和observeOn的一句话

在Rx世界中,通常有两件事要控制并发模型:

In the Rx world, there are generally two things you want to control the concurrency model for:


  1. 订阅的调用

  2. 观察通知

SubscribeOn:指定Observable将在其上运行的调度程序。

SubscribeOn: specify the Scheduler on which an Observable will operate.

ObserveOn:指定观察者将观察此Observable的调度程序

ObserveOn: specify the Scheduler on which an observer will observe this Observable

这篇关于反应式编程优点/缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆