What is Stateless Object in Java?

2019-01-08 08:37发布

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?

8条回答
虎瘦雄心在
2楼-- · 2019-01-08 09:17

An object without state, like instance variables that can change and vary depending on what has already happened to the object

查看更多
淡お忘
3楼-- · 2019-01-08 09:23

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.

查看更多
登录 后发表回答