In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is responsive programming". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In recent years, with the emergence of new languages and technologies such as Go and Node, the status of Java as the leader of server-side development language has been challenged. Although Java's market position will not change in a short period of time, the Java community still sees challenges as opportunities and strives to improve its ability to cope with highly concurrent server-side development scenarios.
In order to deal with the highly concurrent server-side development scenario, in 2009, Microsoft proposed a more elegant way to implement asynchronous programming-Reactive Programming, which we call responsive programming.
Subsequently, each language quickly followed, and each had its own responsive programming implementation. For example, the JavaScript language introduces a similar asynchronous programming method in ES6 through the Promise mechanism. At the same time, the Java community is also developing rapidly. Netflix and LightBend provide technologies such as RxJava and Akka Stream, which make the Java platform have a framework for responsive programming.
At present, we can actually complete server-side development tasks under high concurrency through NIO frameworks such as Mina and Netty, but such technologies are only in the hands of a small number of senior developers, because they are difficult and not suitable for most ordinary developers.
Although there are many companies in the practice of responsive programming, but on the whole, its scope of application is still small. The reason for this is the lack of simple and easy-to-use technologies that need to make responsive programming more popular and integrate technologies like Spring MVC with the services provided by Spring.
On September 28, 2017, Spring 5 was officially released. The biggest significance of the release of Spring 5 is that it takes the popularity of responsive programming a big step forward. At the same time, Spring Reactor, the framework behind Spring 5 responsive programming, has entered the landmark version 3.1.0.
What exactly is responsive programming?
In real life, when we hear someone calling our name, we respond to it, that is, we program based on an event-driven pattern. So this process is actually the event of distribution, and then we, as consumers, make a series of consumption on the event of distribution.
From this point of view, the design of the entire code should be aimed at consumers. For example, when watching a movie, if there are some pictures we don't want to see, close your eyes; if you don't want to hear some voices, cover your ears. In fact, this is the enhanced packaging for consumers, we split the complex logic, and then split it into small tasks for packaging, so there are operations such as lter, map, skip, limit and so on.
01
The relationship between concurrency and parallelism
It can be said that concurrency makes good use of the characteristics of CPU time slices, that is, the operating system selects and runs a task, then runs another task in the next time slice, and sets the previous task to wait. Concurrency does not mean parallelism.
List the following situations in detail.
① sometimes multithreaded execution improves the performance of the application, while sometimes it slows it down. This is evident in the use of Stream API in JDK. If the task is small and we use parallel streams, it will degrade the performance of the application.
In multithreaded programming, ② may open or close multiple threads at the same time, which will incur a lot of performance overhead and reduce the performance of the application.
When a thread is waiting for ③ O at the same time, concurrency may block CPU resources, resulting in not only a long wait for users, but also a waste of CPU computing resources.
④ the situation becomes a little complicated if several threads share a single data. We need to consider whether the state of the data is consistent across threads. In order to achieve data consistency, synchronized or lock related operations are likely to be used.
Now, you know something about concurrency. Concurrency is good, but parallelism is not necessarily achieved. Parallelism is running multiple tasks at the same time on a multi-core CPU or executing a task in multiple blocks at the same time (such as ForkJoin). For single-core CPU, don't think about parallelism.
Add that multithreading actually means concurrency, but parallelism occurs only if these threads are scheduled at the same time and assigned to different CPU for execution. In other words, parallelism is a specific form of concurrency. A task often produces a lot of elements, most of which can only be in the current thread without participating in the operation, so we can ForkJoin them, but this is sometimes very difficult for many programmers to operate and control, and it is difficult to get started. At this time, if you use responsive programming, you can easily send and assign event elements through the provided scheduling API. Each element will be packaged into a task and submitted to the thread pool, and we can select the corresponding thread pool according to whether the task is computational or Imax O-type.
Here, it is important to emphasize that a thread is just an object, and don't think of it as an execution core in CPU. This is a mistake made by many people. CPU time slices switch and execute these threads.
02
How to understand the back pressure in responsive programming
Back pressure, translated by Back Pressure, may be more appropriate to call it back pressure in English literally. First of all, explain the back pressure, it is like drinking a drink with a straw, sucking out the gas in the straw, forming a low pressure in the straw, and then forming the suction of the drink to the direction of the straw, which sucks the drink into the human mouth. We often say that people go up and water flows down. the reason why water occurs is actually caused by gravity. Now the water under the straw rises into the mouth, indicating that there is a reverse pressure downstream pointing upstream, and this reverse pressure is greater than gravity, which can be called back pressure. This is a very intuitive word, backward, backward pressure-Back Pressure.
In the program, that is, during the transmission of the data stream from the upstream source producer to the downstream consumer, if the upstream source production speed is greater than the downstream consumer consumption speed, then the downstream can be thought of as a container that cannot handle the data, and then the data will overflow from the container, similar to the situation in the straw example. Now, all we have to do is provide a solution for this scenario, which is called the back pressure mechanism.
In order to better solve the problem caused by back pressure, let's go back to reality and look at one thing-the dam. During the flood period, the downstream cannot consume so much water at once, and the function of the dam at this time is to intercept the flood and discharge it according to the consumption of the downstream, that is, the back pressure mechanism should be placed in the place where the element producers and consumers are connected, that is, it is the link between the producers and consumers. Then, according to the above description of the dam, the back pressure mechanism should have the ability to carry elements, that is, it must be a container, and the elements should be stored and sent in sequence, so queues are most appropriate here. It is not enough that the back pressure mechanism only plays a load-bearing role, because the upstream carries on the pressure, so the downstream can request elements on demand, and can also limit the current according to the actual situation in the middle, thus the upstream and downstream jointly realize the back pressure mechanism. The API related to back pressure will be introduced in the following content of this book and related supporting videos.
03
Comparison between Reactor and RxJava
With regard to responsive programming, my book, Java programming Methodology: responsive RxJava and Code Design, has been published, so what's the difference between Reactor and RxJava? First of all, I want to make it clear to you that if you are using Java 8, it is recommended to use Reactor 3, while if you are still using Java 6 + or if the function needs to do exception checking, then it is recommended to use RxJava 2.
As can be seen from the figure above, RxJava 2 and Reactor share a set of interface API standard Reactive Streams Commons, which also shows that their ultimate goal is the same, and API is universal, which reduces the cost of learning.
Let's review RxJava again.
So far, RxJava distributions are mainly divided into three major versions: RxJava 3, RxJava 2, and RxJava 1. Unlike RxJava 1, RxJava 3 and RxJava 2 implement the interface definition of Publisher directly through the newly added Flowable type (RxJava 3 is not much different from RxJava 2, so only RxJava 2 is introduced here). At the same time, RxJava 2 still retains the Observable, Completable, and Single in RxJava 1, and introduces the Maybe type, an updated version of Single that supports Optional. Observable in RxJava 1 does not support the back pressure mechanism in RxJava 2, which is a proprietary function of Flowable, but Observable provides convertible API internally. It is important to note that Observable implements the custom ObservableSource interface in RxJava 2.
In Reactor, we can find that both Mono and Flux implement the Publisher interface, and both of them implement the back pressure mechanism. Flux can be used to calibrate the Flowable type in RxJava 2, while Mono can be understood as the back pressure enhanced version of Single in RxJava 2. Later, we will explain it in more depth.
Again, let's take a look at the differences between Reactor and RxJava.
In order to be compatible with Java 1.6 +, RxJava has to define some functional interfaces on its own. You can refer to the interface definition under io.reactivex.functions. Reactor 3 is designed and implemented based on java.util.function provided in JDK.
You can easily convert from java.util.stream.Stream to Flux, or from the latter to the former.
Similarly, you can easily convert CompletableFuture to Mono, and you can easily and safely create Mono based on elements of type Optional.
Reactor 3 can better serve Spring Framework 5 and better adapt to the latest version of JDK.
Finally, let's briefly introduce a few parts of the above picture.
Core is our main research library and the core implementation library of Reactor. Its function is the same as the core implementation of RxJava 2, this book mainly introduces the reactor-core module.
IPC can be thought of as a backpressure-supporting component designed for encode, decode, send (unicast, multicast, or request/response) and service connections. IPC supports Kafka, Netty and Aeron.
Addons includes reactor-adapter, reactor-logback, and reactor-extra. Reactor-adapter can be said to be the bridge connecting Observable, Completable, Flowable, Single, Maybe and Scheduler in RxJava 1amp 2, and can be easily converted to Reactor 3. Similarly, this library is also adapted to Swing/SWT Scheduler and Akka Scheduler. Reactor-logback is used to support Reactor Core's ability to process Logback asynchronously. Reactor-extra provides a lot of mathematical operations for numeric Flux sources.
Reactive Streams Commons is a set of interface API standard shared by RxJava 2 and Reactor.
This is the end of what is responsive programming. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.