Java中的无状态对象是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9735601/
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
What is Stateless Object in Java?
提问by TU_HEO DAKAI
Currently I'm reading "Java concurrency in practice", which contains this sentence:
目前我正在阅读“实践中的 Java 并发”,其中包含这句话:
Since the action of a thread accessing a stateless object can't affect the correctness of operations on other threads, stateless objects are thread-safe.
由于线程访问无状态对象的动作不会影响其他线程上操作的正确性,因此无状态对象是线程安全的。
So, what is stateless object?
那么,什么是无状态对象?
采纳答案by Bozho
Stateless object is an instance of a class without instance fields (instance variables). The class mayhave fields, but they are compile-time constants (static final).
无状态对象是没有实例字段(实例变量)的类的实例。该类可能有字段,但它们是编译时常量(静态最终)。
A very much related term is immutable. Immutable objects may have state, but it does not change when a method is invoked (method invocations do not assign new values to fields). These objects are also thread-safe.
一个非常相关的术语是不可变的。不可变对象可能有状态,但在调用方法时它不会改变(方法调用不会为字段分配新值)。这些对象也是线程安全的。
回答by Tom
An object without state, like instance variables that can change and vary depending on what has already happened to the object
没有状态的对象,例如可以根据对象已经发生的事情而改变和变化的实例变量
回答by Malcolm
If the object doesn't have any instance fields, it it stateless. Also it can be stateless if it has some fields, but their values are known and don't change.
如果对象没有任何实例字段,则它是无状态的。如果它有一些字段,它也可以是无状态的,但它们的值是已知的并且不会改变。
This is a statelessobject:
这是一个无状态对象:
class Stateless {
void test() {
System.out.println("Test!");
}
}
This is also a statelessobject:
这也是一个无状态对象:
class Stateless {
//No static modifier because we're talking about the object itself
final String TEST = "Test!";
void test() {
System.out.println(TEST);
}
}
This object has state, so it is notstateless. However, it has its state set only once, and it doesn't change later, this type of objects is called immutable:
这个对象有状态,所以它不是无状态的。但是,它的状态只设置一次,以后不会改变,这种类型的对象称为immutable:
class Immutable {
final String testString;
Immutable(String testString) {
this.testString = testString;
}
void test() {
System.out.println(testString);
}
}
回答by Marco Mondini
A stateless object is an object that doesn't have any internal state (internal variable)
无状态对象是没有任何内部状态(内部变量)的对象
回答by Massimiliano Peluso
An objects that have absolutely no state then there is no problem with reusing them at this point the question is: if they have absolutely no state why not make all the methods static and never create one at all?
一个完全没有状态的对象,那么在这一点上重用它们没有问题,问题是:如果它们完全没有状态,为什么不让所有方法都静态并且根本不创建一个?
回答by om-nom-nom
The concept of stateless object is highly coupled with concept of side effects. Shortly, that is the object that has no fields underneath which could have different values, dependently on different order of method calls.
无状态对象的概念与副作用的概念高度耦合。简而言之,就是下面没有字段的对象,这些字段可能具有不同的值,具体取决于方法调用的不同顺序。
回答by gazgas
Just a clarification. You can consider your class as stateless in the way that is stated before, even when it has an instance variable as far as this variable is final AND immutable.
只是澄清一下。您可以按照前面所述的方式将您的类视为无状态类,即使它具有实例变量,只要该变量是最终的且不可变的。
If the instance variable is just final but mutable, a List of Strings in example, yes the variable's reference can not be changed but the contents of the List and thus the state of the class can be changed.
如果实例变量只是最终的但可变的,例如一个字符串列表,是的,变量的引用不能改变,但列表的内容和类的状态可以改变。
回答by Harjit Singh
If you can not change any parameter or value etc. of an object, after its creation, then that object is thread-safe.
如果您不能更改对象的任何参数或值等,则在创建后,该对象是线程安全的。
回答by Deepak Agrawal
In simple terms state of object means value of internal variables in that object.
简单来说,对象的状态意味着该对象中内部变量的值。
Stateful - state of object can be changed, means internal values off member variables of that object can be changed
有状态 - 对象的状态可以更改,这意味着可以更改该对象成员变量的内部值
How values can be changed?
如何改变价值观?
By setting the value.
通过设置值。
When can you set that value? When the variable is not final..
你什么时候可以设置这个值?当变量不是最终的..
So, to make the class stateless, make the variable final, so that the value of that variable can't be changed neither in setter not in another method. It can be used only for computing.
因此,要使类无状态,请将变量设为 final,以便该变量的值既不能在 setter 中更改,也不能在其他方法中更改。它只能用于计算。
回答by YourAboutMeIsBlank
Stateless: it has no fields and references no fields from other classes.
The state for a particular computation exists solely in local variables that are stored on the thread's stack and are accessible only to the executing thread.
特定计算的状态仅存在于存储在线程堆栈中的局部变量中,并且只能由执行线程访问。
One thread accessing a method/class cannot influence the result of another thread accessing the same method/class; because the two threads do not share state, it is as if they were accessing different instances.
一个线程访问一个方法/类不能影响另一个线程访问同一个方法/类的结果;因为两个线程不共享状态,就好像它们在访问不同的实例。
Since the actions of a thread accessing a stateless object cannot
affect the correctness of operations in other threads, Stateless objects are threadsafe.