1. When do I have to synchronize? What does synchronization mean? How do I sync?
To maintain the correct visibility across threads, as long as you share non-final variables between several threads, you must use the
Synchronized (or volatile) to ensure that one thread can see changes made by another thread.
Synchronization is necessary for reliable communication between threads and for mutually exclusive access. This is due to the Java language Specification of memory
Model
Summary: To prevent multiple threads from concurrently modifying the same data, synchronization is required, otherwise inconsistent data is created
2, what is called Atomic (atomic operation)?
A Java atomic operation is an operation that is not interrupted. (Is mutual exclusion and visibility?!) )
Is it true that atomic manipulation can actually achieve the thread-safe synchronization effect? There are actually some atomic operations that are not necessarily thread safe
4, Java synchronization Mechanism has 4 ways to achieve: (partial reference to online resources)
①threadlocal②synchronized () ③wait () and notify () ④volatile
Purpose: To solve the access violation of the same variable in multi-threading
3. Don't confuse: synchronous, asynchronous
For example: Ordinary B/s mode (synchronous) Ajax Technology (asynchronous)
Synchronize: Submit request, wait for server processing, and then return to this period the client browser cannot do anything
Asynchronous: Requests are processed through event triggering server processing (which is still something the browser can do)
Visible, he "sync" is not this "sync"--we say that the shared data synchronization in Java (synchronized)
A synchronized object is a behavior (action), and a synchronized object refers to a substance (shared data).
Of
So, under what circumstances are atomic operations not thread-safe? This may be the cause: Java threads allow threads to
Save a copy of the variable in its own memory area. Allows a thread to work with a local private copy rather than using the main memory value every time
To improve performance (I humble opinion: Although the atomic operation is thread-safe, each thread can get the variable (read operation), the
Self-toying with its own copy, the update operation (write operation) because it is not written in the primary, causing other threads not to be visible.
So how do we fix it? Therefore, the Java synchronization mechanism is required.
Java Multithreaded Understanding 2