Java中的无状态对象是什么? [英] What is Stateless Object in Java?
问题描述
目前,我正在阅读"Java并发实践",其中包含以下句子:
Currently I'm reading "Java concurrency in practice", which contains this sentence:
由于线程访问无状态对象的操作不会影响其他线程上操作的正确性,因此无状态对象是线程安全的.
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?
推荐答案
无状态对象是没有实例字段(实例变量)的类的实例.类可能有字段,但是它们是编译时常量(静态final).
Stateless object is an instance of a class without instance fields (instance variables). The class may have 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.
这篇关于Java中的无状态对象是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!