Thread safety object - static or not?

后端 未结 6 2010
故里飘歌
故里飘歌 2021-01-11 15:09

I was recently in an interview and the tech guy asked me about how to make an application thread-safe.

Well, after explaining the lock() correctly, he s

6条回答
  •  日久生厌
    2021-01-11 15:38

    If you have only one instance of a class that shares between multiple threads, it's ok to use normal object. but if you have multiple objects of a class that share between multiple threads, you have to use static object.

    On the other hand, with normal object you can manage concurrency for one instance of a class and with static object you can manage concurrency in the scope of all instances of a class.

提交回复
热议问题