How does Java implement thread synchronization _java
Source: Internet
Author: User
What is thread synchronization?
When you use multiple threads to access the same data, it is very easy to have thread safety issues (for example, more than one thread is manipulating the same data to cause data inconsistency), so we use the synchronization mechanism to solve these problems.
There are two ways to implement a synchronization mechanism:
1. Sync code block:
Synchronized (same data) {} The same data: it is n threads accessing one data at the same time.
2.
Sync Method:
Public synchronized Data Return type method name () {}
is to use synchronized to decorate a method, the method is called a synchronization method. For synchronous methods, without displaying the specified synchronization monitor, synchronization monitor for the synchronization method is this is the object itself (this refers to the object itself is somewhat ambiguous, in fact, the object calling the synchronization method) by using the synchronization method, it is very convenient to turn a class into a thread-safe class with the following characteristics:
1, objects of this class can be accessed by multiple thread security.
2, each thread calls the object's arbitrary methods and 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 be decorated with a method or a block of code, but it cannot be decorated with constructors, attributes, and so on.
Realize the synchronization mechanism note the following: High security, low performance, in multithreading. High performance, low security, in single threaded use.
1, do not synchronize all the methods of the thread-safe class, but only those that change the shared resource method.
2, if the Mutable class has two running environments, the thread and multithreaded environments should provide two versions of the Mutable class: Thread-safe version and thread-unsafe version (no synchronization methods and synchronized blocks). In a single-threaded environment, use thread-safe versions to ensure performance, using thread-safe versions in multiple threads.
Thread Communication:
Why should I use thread communication?
When you use synchronized to decorate a shared resource (both the code block and the synchronization method), when a thread obtains a lock on the shared resource, it can execute the corresponding code snippet until the thread has finished running the code snippet before releasing the lock on the shared resource. Gives other threads the opportunity to perform modifications to the shared resource. When a thread occupies a lock on a shared resource, it is necessary to use the wait () and the Notify ()/notifyall () method for thread communication If another thread wants to get this lock to run.
Three methods in Java.lang.object Wait () notify () Notifyall ()
The wait method results in the current thread waiting until the other thread invokes the synchronization monitor's Notify method or Notifyall method to wake the thread.
Wait (Mills) method
is automatically awakened after a specified time, the current thread that calls the Wait method releases the lock on the synchronization monitor, which can be awakened without notify or Notifyall methods.
Notify ()
Wakes up a single thread waiting on the synchronization monitor, if all threads are waiting on the synchronization monitor, they will choose to wake one of the threads, and the selection is arbitrary and the awakened thread can be executed only after the front thread discards the lock on the synchronization monitor, that is, after the wait method is used.
Notifyall () method
Wakes up all the threads waiting on the synchronization monitor. The awakened thread can only be executed with the current thread discarding the lock on the synchronization monitor
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service