Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to reduce the interface response time in Java multithreaded parallel computing

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to reduce the interface response time in Java multithreaded parallel computing. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The so-called high concurrency is not only in a strategically advantageous position in the architecture, but also requires developers to pay attention to every line of code and every detail in the specific business development.

In the face of performance, we must have our own craftsman spirit, can not compromise with any line of code!

Today, I would like to share with you a tip on how to reduce the interface response time in business development, which is also a common problem in our daily development, that is, how to improve the parallel computing ability of the program.

Sequential execution

Many times, when we develop an interface, we need to call multiple methods, and then assemble the data returned by each method and return it to the front end, such as this:

You can see that I have called four methods here, each of which takes time to simulate, so the delay 100ms returns a string:

It is conceivable that the response time of our API will certainly exceed that of 400ms, and many times of execution will be a little more on 400ms:

Time consuming: 403ms time consuming: 409ms time consuming: 406ms

This is sequential execution, maybe you think it's very Low, but don't you think your code is like this a lot of times?

Thread pool + Future parallel computing

Sequential execution is really slow, so we need parallel execution, that is, we need to call these four methods at the same time. Those who are familiar with Java multithreading know that each method can be executed asynchronously by a separate thread, and then assemble the results of independent thread execution after all execution.

But each call requires the creation of four threads, and thread creation and destruction are expensive, so we have pooling technology.

Thread pool and database connection pool all adopt pooling technology: the created thread is generated in advance and used when it needs to be called. After completing the work, the thread returns to idle state and waits for the arrival of the next task. This avoids the frequent creation and destruction of threads, and improves the response performance of the program.

Therefore, we must make full use of thread pool technology when doing parallel computing. Thread pool technology is mentioned separately in my other article. Students who do not understand can have a preliminary understanding, and the interview is also one of the necessary questions:

Java Thread Pool basic Literacy

Let's go directly to the code:

Thread pool + Future

Run it a few more times to see the output response time:

Time consuming: 108ms time consuming: 105ms time consuming: 105ms

Is the effect obvious?

Directly equivalent to a method call time-consuming, in actual development, if you have an interface after stress testing time around 100ms (most formal companies will require interface performance not to exceed 100ms), then through the thread pool + Future parallel computing way, and can instantly improve your interface performance, no longer have to worry about pressure testing.

Sometimes the test students tell you that the interface stress test is just humiliating? That's a big negation of your professional level.

CompletableFuture of Java8

Future is an interface class in the java.util.concurrent concurrent package, which is used to represent the result of asynchronous execution of a thread. There are the following core methods:

Future.get (): blocks the calling thread until the result of the calculation returns

Future.isDone (): determines whether the thread has finished executing

Future.cancel (): cancels execution of the current thread

What we can know is that Future.get () is a blocking call, and to get the result of thread execution, it must be a Future.get () blocking or while (Future.isDone ()) polling call. This method is called "pull", and responsive programming is now popular, that is, "push". When the thread is finished, just let me know.

Java8 designed a class like CompletableFuture, so let's first look at how to use CompletableFuture to develop the previous code:

CompletableFuture parallel computing

You can see here that the implementation is no different from Future, but CompletableFuture provides many convenient methods, such as allOf,thenApplyAsync in the code, which can combine multiple CompletableFuture into a CompletableFuture, and finally call the join method to block to get the result. The time it takes to call this API multiple times is as follows:

Time consuming: 110ms time consuming: 108ms time consuming: 105ms

There are many methods (50 +) in the CompletableFuture class that can be used by everyone, unlike Future, which is an enhancement of Java's own library to Future.

This is just a brief demonstration of the use of CompletableFuture. In actual development, we need to choose different methods according to different scenarios. I will not introduce API in detail here.

ListenableFuture of Guava

There are always some great companies who come up with powerful open source components that are much better than the official tool classes. Similarly, the ListenableFuture interface in Google's open source Guava extends java's own Future interface and provides a static tool class, Futures.

For the above code, let's see how to do it using ListenableFuture (unlike before, the thread pool needs to be wrapped again in Guava):

It takes time to execute three requests:

Time consuming: 103ms time consuming: 101ms time consuming: 103ms

After reading the above, have you mastered how to reduce the interface response time in Java multithreaded parallel computing? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report