Home > Java > Javagetting Started > Understanding of thread synchronization in java

Understanding of thread synchronization in java

王林
Release: 2019-12-06 13:53:25
Original
2597 people have browsed it

Understanding of thread synchronization in java

First understand what is a thread?

We can run various computer software programs on our computers. Each running program may include multiple independently running threads (Threads).

Thread is a program that runs independently and has its own dedicated running stack. Threads may share some resources with other threads, such as memory, files, databases, etc.

When multiple threads read and write the same shared resource at the same time, conflicts may occur. At this time, we need to introduce a thread "synchronization" mechanism, that is, each thread must be on a first-come, first-served basis, and cannot be crowded together.

Online video tutorial recommendation: java course

Introduction to thread synchronization

The true meaning of thread synchronization coincides with the literal meaning on the contrary. The true meaning of thread synchronization is actually "queuing": several threads need to queue up and operate on shared resources one by one, rather than operating at the same time.

So, regarding thread synchronization, the first point that needs to be kept in mind is: thread synchronization is thread queuing. Synchronization is queuing.

The purpose of thread synchronization is to avoid "synchronous" execution of threads.

Regarding thread synchronization, the second point that needs to be kept in mind is the word "sharing". Only read and write access to shared resources require synchronization. If the resources are not shared, then there is no need for synchronization at all.

Regarding thread synchronization, the third point that needs to be kept in mind is that only "variables" require synchronous access. If the shared resource is fixed, it is equivalent to a "constant", and threads reading the constant at the same time do not need to be synchronized. At least one thread modifies the shared resource. In this case, synchronization between threads is required.

Regarding thread synchronization, the fourth point that needs to be kept in mind is that the code used by multiple threads to access shared resources may be the same code or different codes; regardless of whether the same code is executed, As long as the code of these threads accesses the same mutable shared resource, synchronization is required between these threads.

Recommended related articles and tutorials: java quick start

The above is the detailed content of Understanding of thread synchronization in java. For more information, please follow other related articles on the PHP Chinese website!

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