Instance methods and thread-safety of instance variables

前端 未结 6 791
Happy的楠姐
Happy的楠姐 2021-01-30 23:40

I would like to known if each instance of a class has its own copy of the methods in that class?

Lets say, I have following class MyClass:

p         


        
6条回答
  •  青春惊慌失措
    2021-01-30 23:51

    'Instance Variables are not thread safe' - this statement depends on the context. It is true, if for example you are talking about Servlets. It is because, Servlets create only one instance and multiple threads access it. So in that case Instance Variables are not thread safe.

    In the above simplified case, if you are creating new instance for each thread, then your instance variables are thread safe.

    Hope this answers your question

提交回复
热议问题