Big Brother talk about synchronized keyword usage in Java

Source: Internet
Author: User

Big Brother talk about synchronized keyword usage in JavaMeet synchronized

For the people who write multi-thread, often encounter is concurrency problem, for prone to concurrency problems where the price synchronized basically fix, if not consider performance problems, this can definitely deal with more than 90% of the situation, Additional knowledge, such as read-write locks, is required for performance requirements. This article aims to understand the basic principles of thorough synchronized.

Basic use of synchronized

There are three main functions of synchronized:
(1) Ensure thread-mutually exclusive access synchronization code
(2) Ensure that changes to shared variables are visible in time
(3) Solve the reordering problem effectively.
Syntactically speaking, there are three ways to use synchronized:
(1) General method of modification
(2) Modifying the static method
(3) Modifier code block
  

Package com.paddx.test.concurrent;
Public class Synchronizeddemo {
Public void method() {
synchronized (this) {
System.out.println ("Method 1 start");
}
}
}

For the above method we are easy to know is thread-safe, specifically how to do the thread security, the class through the JAVAP compiled results as follows:

Monitorenter

Each object has a monitor lock. When monitor is occupied, it is locked, and the thread attempts to take ownership of the monitor while executing the monitorenter instruction, as follows:

1. If the monitor has an entry number of 0, the thread enters monitor and then sets the entry number to 1, which is the owner of the Monitor.

2, if the thread already occupies the monitor, just re-enter, then enter the monitor into the number plus 1.

3. If another thread has already occupied monitor, the thread goes into a blocking state until the number of incoming monitor is 0, and then tries to get ownership of monitor again.

Monitorexit

The thread executing the monitorexit must be the owner of the monitor that corresponds to ObjectRef.

When the instruction is executed, the monitor enters the number minus 1, and if the number is 0 after minus 1, the thread exits monitor and is no longer the owner of the Monitor. Other threads that are blocked by this monitor can try to get ownership of the monitor.
Through these two paragraphs, we should be able to clearly see the implementation of the principle of synchronized, synchronized semantic bottom is through a monitor object to complete, in fact, wait/notify and other methods are also dependent on the monitor object, This is why it is only possible to call wait/notify and other methods in a synchronized block or method, or else the cause of the java.lang.IllegalMonitorStateException exception will be thrown.

Principle Summary

Each object has an internal lock or a monitor, called Monitor, and when a method is added to the Synchronized keyword, if a thread wants to execute this method, it first needs to get the object's Monirot permission. Corresponding to the instructions above is to obtain the monitorenter instruction, if an object gets to this instruction, then the number of the monitor's entry is 1, when other threads are acquired again when the object's monitor object is occupied by another thread, then into the blocking state, Know that the thread that occupies this object executes monitorexit, setting the number of entry to 0.

If synchronized is added to a common method, then the valid range is the way that multiple threads execute the same object. The above explanation should be easier to understand, because different objects get different monitor monitors, and naturally there is no occupancy-wait process. If you are loading the static method then you need to get the object's class object, so at this point, no matter how many objects are the same class object, that is, multiple threads have the same monitor occupied-waiting process. Therefore, loading static is valid for the entire class file.

Big Brother talk about synchronized keyword usage in Java

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.