Java keywords transient and volatile summary

The two keywords transient and volatile, one for object serialization and the other for thread synchronization, are relatively high-level topics in Java, so let's briefly summarize them.

transient

transient is a type modifier that can only be used to modify fields. During object serialization, variables marked transient are not serialized.

Example:

class Student{

transient int age; // will not be persisted

String name; // Persistence

}

When the instance object of the class Test is serialized (for example, the instance object t of the Test class is written into the text file t.txt on the hard disk), the content of the variable a will not be saved, but the content of the variable b will be saved.

Reference:
The process of converting the representation of an object into a byte stream is called serialization (also called serialization, serialization), and the reconstruction of an object from a byte stream is called deserialization (also called deserialization). serialization, deserialization). transient provides a language-level method of marking data 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 is forced to reread the value of the member variable from shared memory every time it is accessed by a thread. Also, when member variables change, the thread is forced to write back the changed value to shared memory. In this way, at any time, two different threads always see the same value of a member variable.

Explain the memory mechanism of Java here:

Java uses a main memory to hold the current value of variables, and each thread has its own independent working memory. When a thread accesses a variable, it will copy the value of the variable to its own working memory. In this way, when the thread operates on the variable in its own working memory, the copied value of the variable in the working memory will be the same as the variable in the main memory. value is different.

The Java Language Specification states that, for best speed, threads are allowed to keep private copies of shared member variables and compare them with the original values ​​of shared member variables only when the thread enters or leaves a synchronized block of code.

In this way, when multiple threads interact with an object at the same time, it is necessary to pay attention to the change of shared member variables in time for the threads.

The volatile keyword is a hint to the VM: For this member variable, its private copy cannot be saved, but it should interact directly with the shared member variable.

Recommendation: Use volatile on member variables accessed by two or more threads. Not necessary when the variable to be accessed is already in a synchronized block of code, or is a constant.

Since the use of volatile shields the necessary code optimization in the VM, it is relatively inefficient, so this keyword must be used when necessary.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688779&siteId=291194637