Designing a Thread Safe Class

前端 未结 6 2163
轮回少年
轮回少年 2021-02-05 11:29

When reading the MSDN documentation it always lets you know if a class is thread safe or not. My question is how do you design a class to be thread safe? I am not talking about

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 12:20

    To state that the class is thread safe, you are assserting that the internal data structures in the class won't be corrupted through concurrent access by multiple threads. To make that assertion, you would need to introduce locking (synchronize in Java) around critical sections of code within the class which could potentially lead to corruption of they were executed by multiple concurrent threads.

提交回复
热议问题