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 tune the performance of Java multithreaded Asynchronous calls

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how to tune the performance of Java multithreaded asynchronous calls". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to tune the performance of Java multithreaded asynchronous calls" can help you solve the problem.

Overview

Payment aggregation services of large e-commerce companies all have such scenarios:

Call the verification service to verify whether the order to be generated is legal

Order service generates order (there is no dependency between verification service and order service)

Calling 1 and 2, the payment service implements the function of the payment core.

Complete the aggregate invocation of the payment service by combining steps 1 to 3

If step 1 takes 5 seconds, step 2 takes 3 seconds, and step 3 takes 2 seconds, if you are an architect, require:

1. Please implement the synchronous invocation of micro-service

two。 Please implement the asynchronous invocation of the micro service (using CompletableFuture)

Compare the performance of 1 and 2.

Synchronous and asynchronous invocation

Future class diagram

The deficiency of Future

Future directly expresses the dependence between multiple Future results, which has some defects:

1. Merging two asynchronous calculations into one (the second asynchronous calculation depends on the result of the first), which is not easy to implement with Future.

two。 Wait for all the tasks in the Future collection to be completed

Just wait for the fastest-ending task in the Future collection to complete and return its results

Code address

Https://gitee.com/zjvngvn/mutil-thread

Testpublic class Test {public static void main (String [] args) {/ / synchronous call long start1 = System.currentTimeMillis (); PaymentService.syncPay (); System.out.println ("synchronous payment time:" + (System.currentTimeMillis ()-start1) + "ms"); System.out.println ("="); / / Asynchronous call long start2 = System.currentTimeMillis () PaymentService.asyncPay (); System.out.println ("Asynchronous payment time:" + (System.currentTimeMillis ()-start2) + "ms");}} PaymentServiceimport java.util.concurrent.CompletableFuture;import java.util.concurrent.TimeUnit Public class PaymentService {/ * entry method for asynchronous payment * * @ return * / public static boolean asyncPay () {/ / verify CompletableFuture isValid = CompletableFuture.supplyAsync (()-> CheckService.isValid ()); / / create order CompletableFuture orderSum = CompletableFuture.supplyAsync (()-> OrderService.createOrder () / / payment CompletableFuture money = CompletableFuture.supplyAsync (()-> basePay ()); / / after the above three are completed, proceed to the following anonymous inner class code CompletableFuture.allOf (isValid, orderSum, money) .thenRun (()-> System.out.println ("complete asynchronous payment") .join (); return true } / * entry method of synchronous payment * * @ return * / public static boolean syncPay () {CheckService.isValid (); OrderService.createOrder (); basePay (); System.out.println ("synchronous payment is successful"); / / assuming payment is successful return true } public static int basePay () {int money = 1000; try {TimeUnit.SECONDS.sleep (2);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("payment"); / / assuming payment is successful return money;}} CheckServiceimport java.util.concurrent.TimeUnit Public class CheckService {/ * returns true to indicate that the order process will go down * / public static boolean isValid () {System.out.println ("verify whether the order is legal before it is generated"); try {TimeUnit.SECONDS.sleep (5);} catch (InterruptedException e) {e.printStackTrace () } / / assuming the order is valid, check return true;}} OrderServiceimport java.util.concurrent.TimeUnit;public class OrderService {public static int createOrder () {int orderSum=1; System.out.println ("generate order"); try {TimeUnit.SECONDS.sleep (3);} catch (InterruptedException e) {e.printStackTrace () } / / assuming the number of orders is 1 return orderSum;}} this is the end of the introduction on "how to tune the performance of Java multithreaded asynchronous calls". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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