Threadsafe collection without lock

后端 未结 5 2034
一个人的身影
一个人的身影 2020-12-13 00:59

I am preparing myself for an interview and I came across the followign question. I tried but I could not find anything which can create a class containing thread safe collec

5条回答
  •  被撕碎了的回忆
    2020-12-13 01:53

    The easiest solution is having a field of type string[]. Whenever a caller wants to add a string, create a new array with the new item appended and swap it for the old one.

    This model does not require synchronization. It does not tolerate concurrent writers but it allows for concurrent reading.

提交回复
热议问题