The synchronized of Java concurrent programming

Source: Internet
Author: User

In Java programming, there are 3 different ways to ensure thread safety
1, mutual exclusion synchronization: including synchronized and lock and so on.

2, non-blocking synchronization: such as Atomicinteger Increaseandget () method.

3, no synchronization: such as the threadlocal scheme.

This article describes how to implement synchronization using synchronized.

1, the modification method synchronized static method: Lock added to the class; Synchronized common method: Locks are added to the object. This allows you to know:
    • When a thread is accessing an object's Synchronized method, other threads cannot access the object's other synchronized methods.
    • When a thread is accessing an object's Synchronized method, other threads can access the object's non-synchronized method.
    • If a thread a needs to access the Synchronized method Fun1 of the object Object1, another thread B needs to access the Synchronized method Fun1 of the object object2, even if Object1 and Object2 are the same type, There is no thread-safety problem because they are accessing different objects, so there is no mutex problem.
    • If a thread executes an object's synchronized normal method, and another thread needs to execute the synchronized static method of the class to which the object belongs, there will be no mutex at this time because the access synchronized static method takes a class lock , and access to the synchronized normal method occupies an object lock, so there is no mutex problem.
2. Modify the code block: The code is shown below.
Synchronized (Synobject) {...}
When this block of code is executed in a thread, the thread acquires the lock of the object Synobject, which makes it impossible for other threads to access the code block at the same time. The synobject can be this, which represents the lock that gets the current object, or it can be a property in the class that represents the lock that gets the property. The modified code block is more flexible than the adornment method, and can be synchronized by selecting the code blocks in the method that need to be synchronized. Regardless of whether the synobject is this, the interaction between different methods for the object being synchronized is similar to the one described in 1: if a synchronous block of code for an object is executed, then all of the synchronized code blocks and synchronous methods of the object will be executed before the code block executes. The synchronization method is similar. 3. Synchronized can also modify the class as shown below.
Synchronized (Xxx.class) {...}
Just as synchronized (syncobject) acts like the synchronized common method, synchronized (xx.class) acts like the Synchronized static method, which is a lock on the class. Similarly, synchronized (Xx.class) and synchronized static methods, if they are locks on the same class, are mutually exclusive. 4. For synchronized methods or synchronized code blocks, when an exception occurs, the JVM automatically frees the locks occupied by the current thread, so there is no deadlock caused by an exception. 5, principle Analysis: In Java, each object has a lock tag (monitor), multi-threaded access to an object at the same time, the thread only obtains the lock of the object to access. Each class should also have this tag. This explains why the different synchronized methods and synchronized code blocks of the same object are mutually exclusive because they share a single lock and also explain the mutual exclusion at the class level. For synchronized code blocks, the corresponding bytecode is the byte code corresponding to the Monitorenter and monitorexit;synchronized methods is still synchronized. Monitorenter and Monitorexit bytecode directives require parameters of type reference, and when not explicitly specified in a Java program, the JVM takes an instance object or class object as a lock object. Note: The synchronized sync block is reentrant for the same thread, and does not have the problem of locking itself up.6. The synchronization block blocks the entry of the other threads after the entered thread has finished executing. Java threads are mapped to the native threads of the operating system, and if you want to block or wake up a thread, you need the operating system to do it, which requires a transition from a user state to a kernel mindset, so the state transition takes a lot of processor time. For code-simple synchronization blocks (such as getter () or setter () methods that are synchronized decorated), state transitions can consume longer than the user code executes. So synchronized is a heavyweight (heavyweight) operation in the Java language, and experienced programmers will use it when it is really necessary. The virtual machine itself will also perform some optimizations, such as adding a spin-wait process before notifying the operating system to block threads, avoiding frequent entry into the nuclear mindset.

The synchronized of Java concurrent programming

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.