What is Stateless Object in Java?

前端 未结 10 1338
猫巷女王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 21:53

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

    0 讨论(0)
  • 2020-11-30 22:00

    A stateless object is an object that doesn't have any internal state (internal variable)

    0 讨论(0)
  • 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.
    
    0 讨论(0)
  • 2020-11-30 22:01

    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.

    0 讨论(0)
提交回复
热议问题