Home > Back-end >  About the list reads thread safety problem
About the list reads thread safety problem

Time:10-26

Why get read also locked in the Vector, the feeling is just add lock

CodePudding user response:

Don't lock will cause dirty reads, such as read:
Arr. Get (arr. The size () - 1)

At this time another thread just expanded, haven't fill data, read the above code is empty,

Or another thread in the delete data, the above code might throw exceptions,

CodePudding user response:

reference 1st floor water 2 reply:
unlocked can lead to dirty reads, such as read:
Arr. Get (arr. The size () - 1)

At this time another thread just expanded, haven't fill data, read the above code is empty,

Or another thread in the delete data, the above code might throw exceptions,


Thread expansion ah, you should read the old data expansion just copy the data, the old data is not lost first

CodePudding user response:

Capacity to read the old data no problem,
But the arr. Get (arr. The size () - 1) this is not necessarily read old data oh...

And there are deleted?

CodePudding user response:

CopyOnWriteArrayList read didn't lock, he is to create a new copy of the write operation, when the array before his replacement, whether also can appear you said delete data abnormal problem
  • Related