In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to implement responsive programming in Java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Reactive concept:
Reactive Programming: responsive programming, asynchronous non-blocking is responsive programming, corresponding to imperative programming. Is a declarative programming paradigm based on data flow (data stream) and change delivery. Reactive is not a new technology, and non-blocking programming can be implemented without Reactive, such as the Observer pattern. Another implementation of Reactive is message queuing.
Key points of Reactive:
Declarative programming specifications, data flow, propagating changes, using dynamic arrays or static events.
Spring5 implements part of the Reactive:
Spring WebFlux: Reactive Web (non-blocking servers in genneral)
Spring Web MVC: traditional Servlet Web (serrvlet application in general)
Why reactive programming is needed
1. Imperative programming VS declarative programming
In fact, most of our programmers use imperative programming, which is how computers work. Imperative programming is the abstraction of hardware operation, and the programmer should tell the computer exactly what to do through instructions. Declarative programming is a powerful tool to save programmers, declarative programming is more concerned with what I want (What) than how to do it (How). SQL is a classic declarative language, we describe what we want through SQL, and finally the database engine executes the SQL statement and returns the results to us.
two。 Synchronous programming VS asynchronous programming
When it comes to synchronization and asynchronism, we should talk about the concepts of blocking and non-blocking, because these two sets of concepts are easy to confuse. They describe the same thing, but their concerns are different. Blocking and non-blocking describe the state of the current thread, while synchronization and asynchronism focus on the notification mechanism of the result of method calls. Because the process of calling a method is described from different angles, these two sets of concepts can be combined with each other, that is, the thread state and the result notification mechanism. For example, before JDK1.3, BIO is synchronous plug, JDK1.4 and later NIO are synchronous non-blocking, JDK1.7 and later NIO2 are asynchronous non-blocking mode.
The advantage of synchronous programming is that the code is simple and easy to understand, and the code is executed sequentially, but the disadvantage is that the utilization of CPU is low, and most of it is wasted on IO waiting.
By making full use of CPU resources to execute tasks in parallel, asynchronous programming is much higher than synchronous methods in terms of execution time and resource utilization.
The challenges of synchronous programming:
Traditional applications are usually deployed based on Servlet containers, while Servlet is a thread-based request processing model. From the above discussion, we found that it is usually necessary to set up a large thread pool for good performance, and a large thread pool can cause the following three problems:
1. Additional memory overhead. In Java, each thread has its own stack space, and the default is 1MB. If the thread pool size is set to200, then 200MB memory is required, causing memory waste and slow application startup on the one hand.
Low 2.CPU utilization can lead to low CPU utilization in two ways. On the one hand, after JDK1.2, each platform's JVM implementation uses the 1:1 thread model, which means that an java thread is mapped to a lightweight process, and the number of effective lightweight processes depends on the number of CPU and cores. If the number of threads in Java is much larger than the number of lightweight processes, frequent thread context switching will waste a lot of CPU time. On the other hand, because the traditional remote operations or IO operations are blocking operations, it will cause the execution thread to suspend and unable to perform other tasks, which greatly reduces the utilization of CPU.
3. Fierce competition for resources when increasing the thread pool, other shared resources may become performance bottlenecks, such as database connection resources. It will cause multiple threads to compete for the database connection, making the database connection become the bottleneck of the system.
This is the answer to the question about how to implement responsive programming in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.