What is thread synchronization?
When multiple threads are used to access the same data, thread security issues are very likely to occur (for example, multiple threads are operating on the same data, resulting in data inconsistency ), so we use a synchronous mechanism to solve these problems.
There are two ways to implement the synchronization mechanism:
1. Synchronous Code block:
Synchronized (same data) {} the same data: that is, N threads simultaneously access one data.
2.
Synchronization method:
Public synchronized data return type method name (){}
Is to use synchronized to modify a method, this method is called a synchronization method. For the synchronization method, you do not need to display the specified synchronization monitor. The synchronization monitor of the synchronization method is this, that is, the object itself (the object here is a bit vague, in fact, it is the object that calls the synchronization method) by using the synchronization method, you can easily change a class into a thread-safe class, with the following features:
1. Objects of this class can be securely accessed by multiple threads.
2. After each thread calls any method of the object, the correct result is obtained.
3. After each thread calls any method of the object, the state of the object remains reasonable.
Note: The synchronized keyword can be used to modify methods or code blocks, but not constructors and attributes.
Pay attention to the following points for implementing the synchronization mechanism: high security, low performance, and multithreading. High performance, low security, used in a single thread.
1. Do not synchronize all methods of the thread security class, but only those that will change the shared resource method.
2. If there are two runtime environments for the variable class, two versions should be provided for the variable class when the thread environment and the multi-thread environment: thread-safe version and thread-unsafe version (no synchronization method and synchronization block ). In a single thread environment, the thread-safe version is used to ensure performance, and the thread-safe version is used in multithreading.
Thread Communication:
Why use thread communication?
When synchronized is used to modify a shared resource (synchronization code block and synchronization method), a thread can execute the corresponding code segment after obtaining the lock of the shared resource, the lock on the shared resource is released only after the thread runs the code segment, giving other threads the opportunity to modify the shared resource. When a thread occupies the Lock of a shared resource, if another thread wants to obtain the lock, wait () and Policy ()/policyall () are required () method for thread communication.
Three methods in Java. lang. object: wait () Policy () policyall ()
The wait method causes the current thread to wait until other threads call the y method or the notifyAll method of the synchronization monitor to wake up the thread.
Wait (mills) Method
Wait for the specified time to automatically wake up. The current thread that calls the wait method will release the lock of the synchronization monitor. You do not need to use the Y or notifyAll method to wake it up.
Notify ()
Wake up a single thread waiting on the synchronization monitor. If all threads are waiting on the synchronization Monitor, the system will wake up one of the threads. The choice is arbitrary, the wake-up thread can be executed only when the current thread stops locking the synchronization monitor, that is, the wait method is used.
Yyall () method
Wake up all threads waiting on the synchronization monitor. The wake-up thread can be executed only when the current thread stops locking the synchronization monitor.