Home > Java > javaTutorial > body text

The difference between synchronous and asynchronous in java

(*-*)浩
Release: 2019-12-03 14:24:17
Original
3809 people have browsed it

The difference between synchronous and asynchronous in java

Synchronization: Return to the user only after all operations are completed. 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 closed, the migration process will be interrupted. (Recommended learning: java course)

Asynchronous: Put the user request into the message queue and feed it back to the user. The system migration program has been started. You can close browsing device. 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.

The program processes the data, and the B thread modifies the mathematics processed by the A thread.

Obviously this is caused by global resources. Sometimes in order to solve this problem, priority is given to using local variables and secondarily using synchronized code blocks. For such security considerations, system processing must be sacrificed. Performance, the thread synchronization mechanism is implemented where resource competition is most intense when multi-threads are concurrent.

Synchronization: Thread A wants to request a resource, but this resource is being used by thread B. Because the synchronization mechanism exists, thread A cannot reach it. What should I do? Thread A can only wait.

Asynchronous: Thread A wants to request a resource, but this resource is being used by thread B. Because there is no synchronization mechanism, thread A can still request it, and thread A does not need to wait.

3. Summary :

(1) Synchronization is safe, but due to the existence of synchronization mechanism, the performance will be relatively poor

(2) Asynchronous is unsafe and can easily lead to Deadlock, the death of such a thread may cause the entire process to crash; but because there is no synchronization mechanism, multi-threads execute asynchronously, and performance will be improved.

4. A synchronization mechanism is required only if the following requirements are met at the same time:

(1) Multi-threading

(2) There is competition for shared data

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template