Usage of the synchronized keyword in java Multithreading

Source: Internet
Author: User

Because multiple threads in the same process share memory space, in Java, it is a shared instance. When multiple threads attempt to modify the content of an instance at the same time, a conflict occurs, threads must implement shared mutex to synchronize multiple threads. The simplest synchronization method is to mark a method as synchronized. For the same instance, only one synchronized method can be executed at any time. When a method is executing a synchronized method, if other threads want to execute any of the synchronized methods of this instance, they must wait until the thread executing the synchronized Method exits this method, can be executed in sequence. However, the non-synchronized method is not affected. No matter whether or not the synchronized method is executed, the non-synchronized method can be executed by multiple threads at the same time. In addition, you must note that only the synchronized methods of the same instance can be executed by only one thread at a time, and the synchronized methods of different instances can be concurrent. For example, if class A defines the synchronized Method sync (), different instances of a1.sync () and a2.sync () can be executed by two threads at the same time. The implementation of multi-thread synchronization ultimately depends on the lock mechanism. We can imagine that a shared resource is a room and everyone is a thread. When A wants to enter the room, he must get the door lock. Once A gets the door lock, he locks the door immediately after going in, so B, C, D have to wait outside the door, after A releases the lock, one of B, C, and D grabs the lock (the specific method depends on the implementation of JVM, which can be first served or randomly selected ), then the house is locked. In this way, at most one person can be in the House (using shared resources) at any time ). Java language specifications have built-in support for multithreading. For Java programs, each object instance has a "Lock". Once a thread acquires the lock, if other threads want to obtain the lock, they can only wait for the thread to release the lock. There is only one way to obtain the lock, that is, the synchronized keyword. Example: public class SharedResource {private int count = 0; public int getCount () {return count;} public synchronized void setCount (int count) {this. count = count ;}} synchronization method public synchronized void setCount (int count) {this. count = count;} is actually equivalent to: public void setCount (int count) {synchronized (this) {// here get this lock this. count = count;} // release this lock here} the red part indicates the code segment to be synchronized, which is a "dangerous area". If two or more threads run simultaneously, a conflict occurs, therefore, you need to change the S The internal status of the haredResource. You must first obtain the lock of the SharedResource instance. When the synchronized block is exited, the lock owned by the thread is automatically released, so other threads can obtain the lock again. To improve performance, you do not have to lock this. For example, SharedResource has two independent variables: public class SharedResouce {private int a = 0; private int B = 0; public synchronized void setA (int a) {this. a = a;} public synchronized void setB (int B) {this. B = B ;}} if the entire method is synchronized, setB () and setB () cannot be synchronized during setA (). To improve performance, you can use the locks of different objects: public class SharedResouce {private int a = 0; private int B = 0; private Object sync_a = new Object (); private Object sync_ B = new Object (); public void setA (int a) {synchronized (sync_a) {this. a = a ;}} public synchronized void setB (int B) {synchronized (sync_ B) {this. B = B ;}}

Related Article

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.