How does Javasynchronized work?

Source: Internet
Author: User
1. Why synchronizationJava is a multi-threaded programming language, so it is very important to synchronize java objects or javaclass. The java keyword synchronized or violate can be used to implement synchronization in java multi-threaded environments, and not all things require synchronization. For example,

1. Why synchronization Java is a multi-threaded programming language, so it is very important to synchronize java objects or java classes. The java keyword synchronized or violate can be used to implement synchronization in java multi-threaded environments, and not all things require synchronization. For example,

1. Why synchronization?

Java is a multi-threaded programming language, so it is very important to synchronize java objects or java classes. You can use the java keyword synchronized or violate to implement synchronization in java.

In a java multi-threaded environment, not all things require synchronization. for example, if the shared object is variable, it needs to be synchronized. If the shared object is read-only or immutable, you do not need to use the synchronization technology.

This synchronization is guaranteed by JVM.

2. synchronized Method and synchronized Block

Both methods can synchronize java objects.

First, let's look at the synchronized method. 1) it can provide mutually exclusive access to shared resources to avoid data competition. 2) It can prevent the code from being reordering by the compiler, resulting in concurrency problems. 3) provides the lock and unlock methods. before entering the place where synchronization is to be synchronized, you must first obtain the lock to read things in memory. When releasing the lock, write the data back to memory.

Let's take a look at the synchronized block. It does not lock the entire object or class, but only synchronizes the lock for specific parts. In other places that do not need to be synchronized, concurrent access and execution are allowed, this will speed up the system's efficiency.

Therefore, we strongly recommend that you use synchronized block3. synchronized Granularity

In the following example, synchronization is not actually effective.

public class Counter{private static count = 0;public static synchronized  getCount(){  return this.count;}public synchoronized setCount(int count){   this.count = count;}}


GetCount is a lock on the Coutner. class, while setCount is a lock on the object (this. Therefore, they can perform concurrent operations, resulting in problems in setting and reading the count value.

4. Key Points of synchronized

1) The key to synchronized is to provide mutex access to java shared resources in a multi-threaded environment. It ensures the synchronization of multiple threads using the same lock,

2) The key of synchronized can only be used in the synchronized method or synchronized block. You cannot perform the synchronized operation on the variable.

3) When a thread enters synchronized, it must obtain the corresponding lock, and when it leaves, it must release the corresponding lock. The lock will be released when the thread completes synchronization or when an error or exception occurs.

4) When Java Thread implements a syncrhonized Java method, it will require obtaining the object-Level Lock. when entering the Static synchronized method, it needs to obtain the class-Level Lock.

5) the Java Synchronized keyword can be reinjected.

6) if the java synchronized block synchronization object is null, JAVA will throw an NullPointerException.

7) synchronized can only be synchronized on the same JVM.

8) using synchronized may cause performance degradation.

9) Java synchronized block is better than synchronized method

10) after Java 5, all variables using violate are atomic.

11) synchronized cannot be used in constructors and may cause programming errors.

12) synchronized cannot use variables.

13) the Java class library Calendar and SimpleDataFormat are not thread-safe, so another synchronization mechanism is required.

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.