A method for synchronizing Java implementations

Source: Internet
Author: User

What is thread synchronization?
When multiple threads are used to access the same data, it is very easy to have thread-safety problems (such as if more than one thread is manipulating the same data to cause inconsistent data), so we use a synchronization mechanism to solve these problems.

There are two ways to implement a synchronization mechanism :
1.

Synchronizing code blocks:
Synchronized (same data) {} The same data: N threads access one data at the same time.

2.
Synchronization method:
Public synchronized Data Return type method name () {}
is to use synchronized to decorate a method, the method is called a synchronous method. For a synchronous method, there is no need to display the specified synchronization monitor, the synchronization monitor of the synchronous method is this that is the object itself (in this case the object itself is a bit vague, it is actually called the object of the synchronization method) by using the synchronous 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 threads safely.
2, after each thread invokes any method of the object, it will get the correct result.
3, after each thread invokes any method of the object, the object's state remains in a reasonable state.
Note: The Synchronized keyword can be used to modify a method, or to decorate a block of code, but not to decorate constructors, properties, and so on.
Realize the synchronization mechanism note the following points: High security, low performance, in multi-threaded use. High performance, low security, in single-threaded use.
1, do not synchronize all methods of the thread-safe class, only those that change the shared resource method.
2, if the Mutable class has two operating environments, the thread and multithreaded environments should provide two versions of the mutable class: The thread-safe version and the thread-unsafe version (there is no synchronization method and synchronization block). In a single-threaded environment, a thread-safe version is used in multi-threading to ensure performance by using an unsafe version of the threads.

Thread Communication :
Why use thread communication?
When you use synchronized to decorate a shared resource (in the case of synchronized blocks of code and synchronous methods), when a thread obtains a lock on a shared resource, it can execute the appropriate code snippet until the thread has run out of the code snippet before releasing the lock on that shared resource. Give other threads the opportunity to perform modifications to the shared resource. When a thread occupies a lock on a shared resource, it needs to use the wait () and notify ()/notifyall () methods for thread communication if another thread also wants to get the lock run.
Three ways to Java.lang.object Wait () notify () Notifyall ()
The wait method causes the current thread to wait until another thread calls the synchronization Monitor's Notify method or the Notifyall method to wake the thread.
Wait (Mills) method
is automatically awakened after a specified time, the current thread that calls the Wait method frees the sync Monitor's lock and can wake it without the Notify or Notifyall method.
Notify ()
Wakes a single thread waiting on the synchronization monitor, and if all threads are waiting on the synchronization monitor, it chooses to wake up one of the threads, and the selection is arbitrary, and the wake-up 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. Only when the current thread abandons the lock on the synchronization monitor can you perform a wake-up

A method for synchronizing Java implementations

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.