What are the ways Java implements thread synchronization

  • 2020-04-01 01:07:51
  • OfStack

What is thread synchronization?
When using multiple threads to access the same data, it is very easy to have thread-safety problems (such as inconsistent data when multiple threads are working on the same data), so we use synchronization to solve these problems.

There are two ways to implement a synchronization mechanism :
1. Synchronized code block:
Synchronized (same data){} same data: that is, N threads access a data at the same time.
2.
Synchronization method:
Public synchronized data return type method name (){}
When a method is decorated with synchronized, it is called a synchronized method. For synchronization method, need not specified synchronous monitor, synchronization method of synchronous monitor is this also is the object itself (here refers to the object itself is a bit vague, is called the synchronous method of the object) by using a synchronization method, can be very convenient to certain into thread-safe class, has the following features:
Objects of this class can be accessed by more than one thread safely.
2, after each thread invokes any method of the object, it gets the correct result.
3. After each thread invokes any method of the object, the object state remains in a reasonable state.
Note: the synchronized keyword can modify either methods or code blocks, but not constructors, properties, etc.
Implement synchronization mechanism to note the following: high security, low performance, in multi-threading. High performance, low security, used in a single thread.
1. Do not synchronize all methods of a thread-safe class, only those that change the methods of a Shared resource.
2. If the mutable class has two running environments, both the threaded and multithreaded environments should provide two versions of the mutable class: the thread-safe version and the thread-unsafe version (without synchronized methods and synchronized blocks). In single-threaded environments, thread-unsafe versions are used for performance, and thread-safe versions are used in multi-threaded environments.

Thread communication :
Why use thread communication?
When using synchronized to modify a Shared resource (synchronized code block and synchronized methods two cases), when a thread for Shared resources after the lock can perform the corresponding code segment, it was not until after the thread running the code to release the lock of the Shared resource, a chance for another thread to execute to modify the Shared resources. When a thread holds a lock on a Shared resource, it USES the wait() and notify()/notifyAll() methods to communicate if another thread wants to acquire the lock.
Three methods in java.lang.object wait() notify() notifyAll()
The wait method causes the current thread to wait until another thread invokes the notify or notifyAll methods of the synchronization monitor to wake up the thread.
Wait (mills) method
Both automatically wake up after waiting for a specified time, and the current thread calling the wait method releases the lock on the synchronization monitor, which can be woken up without the notify or notifyAll methods.
Notify ()
Wakes up a single thread that is waiting on the synchronization monitor. If all threads are waiting on the synchronization monitor, one of them will be selected to wake up.
NotifyAll () method
Wakes up all threads waiting on the synchronization monitor. Only after the current thread abandons the lock on the synchronization monitor can the awakened thread execute

Related articles: