Home  >  Article  >  Java  >  What is the difference between synchronous and asynchronous in java?

What is the difference between synchronous and asynchronous in java?

青灯夜游
青灯夜游Original
2019-12-26 16:57:086339browse

What is the difference between synchronous and asynchronous in java?

Concept:

1. Synchronization: All operations are completed before being returned to the user. In this way, the user waits online for too long, giving the user a stuck feeling (that is, when the system is being migrated, if you click Migrate, the interface will not move, but the program is still executing, giving the user a stuck feeling). In this case, the user cannot close the interface. If it is closed, the migration process will be interrupted.

2. Asynchronous: Put the user request into the message queue and feedback it to the user. The system migration program has been started and you can close the browser. Then the program slowly writes to the database. This is asynchronous. But the user does not feel stuck and will tell you that the system has responded to your request. You can close the interface.

[Recommended learning: Java video tutorial]

Synchronization means that all operations are completed before the results are returned to the user; that is, after writing to the database, the corresponding User, the user experience is not good.

Asynchronously, there is no need to wait for all operations to be completed before responding to user requests; that is, responding to user requests first, and then slowly writing to the database, which provides a better user experience.

Examples of synchronous and asynchronous operations:

1. Synchronization:

For example, bank transfer systems, database save operations, etc. all use synchronization. Interactive operations.

2. Asynchronous:

In order to avoid a large number of database operations in a short period of time, the caching mechanism, that is, the message queue, is used. Put the data into the message queue first, and then slowly write it to the database.

Introducing the message queue mechanism, although it can ensure a quick response to user requests, does not shorten the time of my data migration (that is, it takes 1 hour to write 800,000 pieces of data to mysql. After using redis, It still takes an hour, just to ensure a quick response to the user's request. After the user enters the http url request, he can close the browser and do other things. If redis is not used, the browser cannot be closed).

The above is the detailed content of What is the difference between synchronous and asynchronous in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn