Java keywords volatile and synchronized, javavolatile keywords

Source: Internet
Author: User
Tags java keywords

Java keywords volatile and synchronized, javavolatile keywords

 

Volatile is a variable modifier, while synchronized acts on a piece of code or method. The code for the following three get methods:

1 int i1;2 int geti1() {return i1;}3 4 volatile int i2;5 int geti2() {return i2;}6 7 int i3;8 synchronized int geti3() {return i3;}

Geti1 () gets the i1 value stored in the current thread. Multiple Threads have multiple i1 variable copies, and these i1 variables can be different from each other. In other words, another thread may have changed the i1 value in its thread, which may be different from the i1 value in the current thread. In the Java memory model, there is main memory (main memory area), which stores the current "exact value" of the variable, and each thread also has its own memory (such as registers ). For performance, a thread saves copies of the variables to be accessed in its own memory. In this case, the value of the same variable may be different from that of the memory of another thread or the value of the main memory. Therefore, there is a possibility that the i1 value of main memory is 1, The i1 value in thread 1 is 2, and the i1 value in thread 2 is 3, this happens when both thread 1 and thread 2 change their i1 values, and the change has not been passed to main memory or other threads.

Geti2 () obtains the i2 value of main memory.If a variable is declared as volatile, it means that the variable is modified by other threads at any time, so it cannot be cached in the memory of the thread.In other words, a variable must be synchronized in all threads after being modified by volatile. Any thread changes its value and all other threads obtain the same value immediately. Therefore, volatile variable access requires more resources than normal variables, because the thread has its own variable copy efficiency.

The geti3 () method is modified by synchronized. When synchronized is used to modify a method or a loan, only one thread can execute the code at most at the same time. Since the volatile keyword has already implemented data synchronization between threads, what should we do with synchronized? When two concurrent threads access the synchronized (this) Synchronization loan in the same object, only one thread can be executed within a time period. The other thread must wait until the current thread finishes executing this code block to execute this code block. However, when one thread accesses a synchronized (this) synchronization code block of the object, the other thread can still access the non-synchronized (this) synchronization code block of the object. In particular, when a thread accesses a synchronized (this) synchronization code block of the object, access by other threads to all other synchronized (this) synchronization code blocks of the object will be blocked. When a thread accesses a synchronized (this) synchronization code block of an object, it obtains the object lock of this object, access by other threads to all synchronization code parts of the object is temporarily blocked.

To sum up the differences:

1. volatile is a variable modifier, while synchronized acts on a piece of code or method.

2. volatile only synchronizes the value of a variable between the thread memory and main memory (main memory), while synchronized synchronizes the value of all variables by locking and unlocking a monitor. Obviously, synchronized consumes more resources than volatile.

 

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.