Java keyword Transient and volatile summary (RPM)

Source: Internet
Author: User
Tags object serialization volatile

Transient and volatile two keywords one for object serialization, one for thread synchronization, is a relatively high-level topic in Java, a simple summary.

Transient

Transient is a type modifier that can only be used to decorate a field. In the process of object serialization, variables marked as transient are not serialized.

Example:

Class Test {

transient int A; Will not be persisted

int b; Persistence of

}

When an instance object of class test is serialized (such as writing the instance object T of the test class to the hard disk's text file t.txt), the contents of variable A are not saved and the contents of variable B are saved.

Reference:
The process of converting an object's representation into a byte stream is called serialization (also known as serializing, serialization), and rebuilding an object from a byte stream is called crossdress (also known as deserialization, deserialization). Transient provides a language-level tag data method for data that should not be serialized.

Volatile
Volatile is also a variable modifier and can only be used to modify variables. A volatile-modified member variable forces the value of the member variable to be reread from shared memory each time it is accessed by the thread. Also, when a member variable changes, forcing the thread to write the change back to the shared memory. So at any moment, two different threads always see the same value for a member variable.

Explain Java's memory mechanism here:

Java uses one main memory to hold the current value of the variable, and each thread has its own working memory. When a thread accesses a variable, it copies the value of the variable into its own working memory, so that when the thread operates on a variable in its own working memory, the value of the variable copied in the working memory is different from the value of the variable in the main memory.

The Java language specification states that for optimal speed, a thread is allowed to save a private copy of a shared member variable, and is compared to the original value of a shared member variable only if the thread enters or leaves the synchronized code block.

This way, when multiple threads interact with an object at the same time, it is important to notice that the thread gets the changes to the shared member variables in a timely manner.

The volatile keyword is a hint to the VM: You cannot save its private copy for this member variable, but you should interact directly with the shared member variable.

Usage Recommendation: Use volatile on member variables accessed by two or more threads. You do not have to use the variable you want to access when it is already in a synchronized code block, or is a constant.

It is inefficient to use volatile to mask the necessary code optimizations in the VM, so use this keyword when necessary.

Java keyword Transient and volatile summary (RPM)

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.