Java 什么是瞬态和挥发性修饰符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3544919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:43:56  来源:igfitidea点击:

What are Transient and Volatile Modifiers?

javaserializationstoragemodifier

提问by Vijay Bhaskar Semwal

Can someone explain what the transientand volatilemodifiers mean in Java?

有人能解释一下Java 中的transientvolatile修饰符是什么意思吗?

回答by Raynos

Volatile means other threads can edit that particular variable. So the compiler allows access to them.

Volatile 意味着其他线程可以编辑该特定变量。所以编译器允许访问它们。

http://www.javamex.com/tutorials/synchronization_volatile.shtml

http://www.javamex.com/tutorials/synchronization_volatile.shtml

Transient means that when you serialize an object, it will return its default value on de-serialization

瞬态意味着当您序列化一个对象时,它将在反序列化时返回其默认值

http://www.geekinterview.com/question_details/2

http://www.geekinterview.com/question_details/2

回答by Stephen C

The volatileand transientmodifiers can be applied to fields of classes1irrespective of field type. Apart from that, they are unrelated.

volatiletransient改性剂可以被施加到的类的字段1,不论字段类型。除此之外,他们没有任何关系。

The transientmodifier tells the Java object serialization subsystem to exclude the field when serializing an instance of the class. When the object is then deserialized, the field will be initialized to the default value; i.e. nullfor a reference type, and zero or falsefor a primitive type. Note that the JLS (see 8.3.1.3) does not say what transientmeans, but defers to the Java Object Serialization Specification. Other serialization mechanisms maypay attention to a field's transient-ness. Or they may ignore it.

transient修饰符告诉Java对象序列化子系统序列化类的实例时排除领域。当对象被反序列化时,该字段将被初始化为默认值;即null对于引用类型,零或false原始类型。请注意,JLS(参见8.3.1.3)没有说明什么transient意思,而是遵循Java 对象序列化规范。其他序列化机制可能会关注字段的transient-ness。或者他们可能会忽略它。

(Note that the JLS permits a staticfield to be declared as transient. This combination doesn't make sense for Java Object Serialization, since it doesn't serialize statics anyway. However, it could make sense in other contexts, so there is some justification for not forbidding it outright.)

(请注意,JLS 允许将static字段声明为transient。这种组合对于 Java 对象序列化没有意义,因为它无论如何都不会序列化静态。但是,它在其他上下文中可能有意义,因此有一些理由不完全禁止。)

The volatilemodifier tells the JVM that writes to the field should always be synchronously flushed to memory, and that reads of the field should always read from memory. This means that fields marked as volatile can be safely accessed and updated in a multi-thread application without using native or standard library-based synchronization. Similarly, reads and writes to volatile fields are atomic. (This does not apply to >>non-volatile<< longor doublefields, which may be subject to "word tearing" on some JVMs.) The relevant parts of the JLS are 8.3.1.4, 17.4and 17.7.

volatile修饰符告诉JVM是写入到现场应始终同步刷新到内存中,并读取该领域应始终从内存中读取。这意味着可以在多线程应用程序中安全地访问和更新标记为 volatile 的字段,而无需使用本机或基于标准库的同步。同样,对 volatile 字段的读取和写入也是原子的。(这不适用于 >>non-volatile<<longdouble字段,它们在某些 JVM 上可能会受到“字词撕裂”的影响。)JLS 的相关部分是8.3.1.417.417.7



1 - But not to local variables or parameters.

1 - 但不是局部变量或参数。

回答by Rajesh Dixit

volatileand transientkeywords

volatiletransient关键字

1) transientkeyword is used along with instance variables to exclude them from serialization process. If a field is transientits value will not be persisted.

1)transient关键字与实例变量一起使用以将它们从序列化过程中排除。如果一个字段是transient它的值将不会被持久化。

On the other hand, volatilekeyword is used to mark a Java variable as "being stored in main memory".

另一方面,volatile关键字用于将 Java 变量标记为“存储在主内存中”。

Every read of a volatilevariable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatilevariable will be written to main memory, and not just to the CPU cache.

每次读取volatile变量都会从计算机的主内存中读取,而不是从 CPU 缓存中读取,并且每次对volatile变量的写入都将写入主内存,而不仅仅是 CPU 缓存。

2) transientkeyword cannot be used along with statickeyword but volatilecan be used along with static.

2)transient关键字不能与static关键字一起使用,但volatile可以与static.

3) transientvariables are initialized with default value during de-serialization and there assignment or restoration of value has to be handled by application code.

3)transient变量在反序列化期间使用默认值初始化,并且值的分配或恢复必须由应用程序代码处理。

For more information, see my blog:
http://javaexplorer03.blogspot.in/2015/07/difference-between-volatile-and.html

更多信息请看我的博客:http:
//javaexplorer03.blogspot.in/2015/07/difference-between-volatile-and.html

回答by Vishal Sheth

Transient :

短暂的 :

First need to know where it needed how it bridge the gap.

首先需要知道它需要如何弥合差距。

1) An Access modifier transient is only applicable to variable component only. It will not used with method or class.

1) 访问修饰符瞬态仅适用于可变组件。它不会与方法或类一起使用。

2) Transient keyword cannot be used along with static keyword.

2) Transient 关键字不能与 static 关键字一起使用。

3) What is serialization and where it is used? Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes to be used for persisting (e.g. storing bytes in a file) or transferring (e.g. sending bytes across a network). In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. Before understanding the transient keyword, one has to understand the concept of serialization. If the reader knows about serialization, please skip the first point.

3)什么是序列化以及它在哪里使用?序列化是使对象的状态持久化的过程。这意味着对象的状态被转换成字节流以用于持久化(例如在文件中存储字节)或传输(例如通过网络发送字节)。同样,我们可以使用反序列化从字节中恢复对象的状态。这是 Java 编程中的重要概念之一,因为序列化主要用于网络编程。需要通过网络传输的对象必须转换为字节。在理解transient关键字之前,必须先了解序列化的概念。如果读者知道序列化,请跳过第一点。

Note 1) Transient is mainly use for serialzation process. For that the class must implement the java.io.Serializable interface. All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

注 1) Transient 主要用于序列化过程。为此,该类必须实现 java.io.Serializable 接口。类中的所有字段都必须是可序列化的。如果字段不可序列化,则必须将其标记为瞬态。

Note 2) When deserialized process taken place they get set to the default value - zero, false, or null as per type constraint.

注 2)当反序列化过程发生时,它们被设置为默认值 - 根据类型约束为零、假或空。

Note 3) Transient keyword and its purpose? A field which is declare with transient modifier it will not take part in serialized process. When an object is serialized(saved in any state), the values of its transient fields are ignored in the serial representation, while the field other than transient fields will take part in serialization process. That is the main purpose of the transient keyword.

注 3)Transient 关键字及其用途?使用瞬态修饰符声明的字段不会参与序列化过程。当一个对象被序列化(保存在任何状态)时,其瞬态字段的值在序列表示中被忽略,而瞬态字段以外的字段将参与序列化过程。这就是transient关键字的主要目的。