how multiple threads invoke singleton object's method and work on them?

前端 未结 4 923
無奈伤痛
無奈伤痛 2021-01-29 20:05

I have multiple threads running which access singleton object and call its method and pass objects in it. In the method I do some calculations only on recieved object. I have he

4条回答
  •  Happy的楠姐
    2021-01-29 20:26

    Every thread has its own copy of the Object o and so even if multiple threads call the method at the same time, each method call of Thread will have its own stack allocated and the logic will apply there.

    Why is it Thread Safe?

    Thread Stacks are private to them and are by default thread safe as no other thread can enter in others stack.

    NOTE: You can say that this Singleton of yours is Thread safe but not that the application is Thread safe as it also depends on whether the Object o being passed is itself thread safe or not. But as far as safety of Singleton is concerned it is thread safe.

提交回复
热议问题