What is Stateless Object in Java?

前端 未结 10 1444
猫巷女王i
猫巷女王i 2020-11-30 21:32

Currently I\'m reading \"Java concurrency in practice\", which contains this sentence:

Since the action of a thread accessing a stateless object can\'

10条回答
  •  半阙折子戏
    2020-11-30 22:00

    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.
    

提交回复
热议问题