java 反应式编程的优点/缺点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42062199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reactive Programming Advantages/Disadvantages
提问by prranay
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.
我一直在研究和尝试使用 Reactor 和 RxJava 的 Reactive Style 编码。我确实明白,与单线程执行相比,反应式编码可以更好地利用 CPU。
Is there any concrete comparison between reactive programming vs imperative programming in web based applications?
在基于 Web 的应用程序中,反应式编程与命令式编程之间有什么具体的比较吗?
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?
反应式编程的优缺点是什么?
Is there any statistical benchmark?
有没有统计基准?
回答by Ravindra Ranwala
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 twoand part threefor further details.
好吧,响应式编程意味着您正在异步执行所有 IO 绑定任务,例如网络调用。例如,您的应用程序调用外部 REST API 或数据库,您可以异步执行该调用。如果您这样做,您当前的线程不会阻塞。您可以通过生成一个或几个线程来处理大量请求。如果您遵循阻塞方法,则需要有一个线程来处理每个请求。您可以参考我的多部分博客文章第一部分、第二部分和第三部分以获取更多详细信息。
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 可以让您编写更简单、可组合和可读的异步代码。此外,RxJava 为您提供了许多强大的运算符,例如 Map、Zip 等,它们使您的代码更加简单,同时由于不相互依赖的不同任务的并行执行而提高了性能。
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 不是另一个带有一组运算符的 Observer 实现,而是它为您提供了非常方便的良好错误处理和重试机制。
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.
但是我没有使用命令式编程方法对 RxJava 进行任何基准测试,以在统计上赞扬你。但我非常确定 RxJava 应该比阻塞机制产生更好的性能。
Update
更新
Since I gathered more experience over time, I thought of adding more points to my answer.
随着时间的推移,我积累了更多的经验,我想在我的答案中添加更多的分数。
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.
基于文章,ReactiveX 是一个使用可观察序列编写异步和基于事件的程序的库。我认为您首先要阅读这篇介绍性文章。
These are some properties of reactive systems: Event Driven, Scalable, Resilient, Responsive
这些是反应式系统的一些特性:事件驱动、可扩展、弹性、响应
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.
谈到 RxJava,它为程序员提供了两个主要工具。首先,它使用一组丰富的运算符(例如 zip、concat、map 等)提供了一个很好的可组合 API。这会产生更简单易读的代码。当谈到代码时,可读性和简单性是最重要的属性。其次,它提供了出色的抽象,使并发成为声明性的。
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.
一个流行的误解是 Rx 默认是多线程的。老实说,Rx 默认是单线程的。如果你想异步地做事情,那么你必须通过传递相关的调度程序来明确地告诉它使用subscribeOn
和observeOn
操作符。RxJava 为您提供线程池来执行异步任务。有许多调度程序,例如 IO、计算等。顾名思义,IO 调度器最适合 IO 密集型任务,例如网络调用等。相反,计算调度器适用于 CPU 密集型计算任务。你也可以用 RxJava 连接你自己的 Executor 服务。内置的调度器主要帮助你摆脱维护自己的 Executor 服务,让你的代码更简单。
Finally a word on subscribeOn and observeOn
最后说一下 subscribeOn 和 observeOn
In the Rx world, there are generally two things you want to control the concurrency model for:
在 Rx 世界中,通常有两件事要控制并发模型:
- The invocation of the subscription
- The observing of notifications
- 订阅的调用
- 通知的观察
SubscribeOn: specify the Scheduler on which an Observable will operate.
SubscribeOn:指定 Observable 将在其上运行的调度程序。
ObserveOn: specify the Scheduler on which an observer will observe this Observable
ObserveOn:指定观察者将在其上观察此 Observable 的调度程序
回答by Krishna Ganeriwal
Disadvantages
缺点
- More memory intensive to store streams of data most of the times (since it is based on streams over time).
- Might feel unconventional to learn at start(needs everything to be a stream).
- Most complexities have to be dealt with at the time of declaration of new services.
Lack of good and simple resources to learn.
Often confused to be equivalent to Functional Reactive Programming.
- 大多数时候存储数据流需要更多的内存(因为它是基于流的)。
- 一开始学习可能会感觉非常规(需要一切都是流)。
- 大多数复杂性必须在宣布新服务时处理。
缺乏好的和简单的学习资源。
经常混淆为等同于函数式反应式编程。
回答by paul
Apart what they already response regarding no blocking features, another great feature to use Reactive programing, is the important use of backpressure. Normally is used in situations where your publisher emit more information than your consumer can process.
除了他们已经对无阻塞特性做出回应之外,使用反应式编程的另一个重要特性是背压的重要用途。通常用于发布者发布的信息多于消费者处理能力的情况。
So having this mechanism you can control the flow of traffic between both and avoid the nasty out of memory problems.
因此,拥有这种机制,您可以控制两者之间的流量并避免令人讨厌的内存不足问题。
You can see some practicle examples of Reactive programing here https://github.com/politrons/reactive
你可以在这里看到一些反应式编程的实践示例https://github.com/politrons/reactive
And about back pressure here https://github.com/politrons/Akka/blob/master/src/main/scala/stream/BackPressure.scala
关于背压https://github.com/politrons/Akka/blob/master/src/main/scala/stream/BackPressure.scala
By the way, the only disadvantage about reactive programing, is the curve of learning because you′re changing paradigm of programing. But nowadays all important companies respect and follow the reactive manifesto http://www.reactivemanifesto.org/
顺便说一下,响应式编程的唯一缺点是学习曲线,因为你正在改变编程范式。但是现在所有重要的公司都尊重并遵循反应式宣言http://www.reactivemanifesto.org/
回答by kafkas
Reactive Programming is a style of micro-architecture involving intelligent routing and consumption of events.
反应式编程是一种涉及智能路由和事件消费的微架构风格。
Reactive is that you can do more with less, specifically you can process higher loads with fewer threads.
反应式是你可以用更少的东西做更多的事情,特别是你可以用更少的线程处理更高的负载。
Reactive types are not intended to allow you to process your requests or data faster.Their strength lies in their capacity to serve more request concurrently, and to handle operations with latency, such as requesting data from a remote server, more efficiently.
响应式类型并不是为了让您更快地处理请求或数据。它们的优势在于它们能够同时处理更多请求,并更有效地处理有延迟的操作,例如从远程服务器请求数据。
They allow you to provide a better quality of service and a predictable capacity planning by dealing natively with time and latency without consuming more resources.
它们允许您通过原生处理时间和延迟而不消耗更多资源来提供更好的服务质量和可预测的容量规划。
From
https://blog.redelastic.com/what-is-reactive-programming-bc9fa7f4a7fchttps://spring.io/blog/2016/06/07/notes-on-reactive-programming-part-i-the-reactive-landscapehttps://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1
来自
https://blog.redelastic.com/what-is-reactive-programming-bc9fa7f4a7fc https://spring.io/blog/2016/06/07/notes-on-reactive-programming-part-i-the-反应式景观https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1
回答by EigenFool
Advantages
好处
- Cleaner code, more concise
- Easier to read (once you get the hang of it)
- Easier to scale (pipe any operation)
- Better error handling
- Event-driven inspired -> plays well with streams (Kafka, RabbitMQ,etc)
- Backpressure (client can control flow)
- 代码更简洁,更简洁
- 更容易阅读(一旦你掌握了窍门)
- 更容易扩展(管道任何操作)
- 更好的错误处理
- 受事件驱动启发 -> 与流(Kafka、RabbitMQ 等)配合良好
- 背压(客户端可以控制流量)
Disadvantages
缺点
- Can become more memory intensive in some cases
- Somewhat steep learning curve
- 在某些情况下可能会变得更加内存密集
- 有点陡峭的学习曲线
回答by Alexei Kaigorodov
Reactive programming is a kind of imperative programming. Reactive programming is a kind of parallel programming. You can achieve performance gain over single threaded execution only if you manage to create parallel branches. Will they executed by multiple threads, or by reactive constructs (which in fact are asynchronous procedures), does not matter.
响应式编程是一种命令式编程。响应式编程是一种并行编程。仅当您设法创建并行分支时,您才能获得比单线程执行更高的性能。它们是由多个线程执行,还是由反应式构造(实际上是异步过程)执行,并不重要。
The single advantage of reactive programming over multithreaded programming is lower memory consumption (each thread requires 0.5...1 megabyte). The disadvantage is less easy programming.
响应式编程相对于多线程编程的唯一优势是内存消耗更低(每个线程需要 0.5...1 兆字节)。缺点是不太容易编程。