An array of lists, is it thread safe to resize a list?

Started by
4 comments, last by Alberth 5 years, 8 months ago

Hey, I've created an array of lists


public List<Cube> [] cubes;

This array contains a whole map of cubes compressed with RLE (Run-length encoding) to save memory and I'm wondering is it thread safe to resize a single List inside that array while there is only one thread editing that particular list but multiple threads editing other lists inside the same array?

Advertisement

The array only holds references (pointers) to the lists - the list contents are not stored in RAM anywhere near the array.  If your threads are ONLY modifying different Lists, then it will be fine.

If your threads are accessing the cubes array itself and one thread decides to reassign an entry in the array, THAT could cause problems.

If you ever have two threads that access the same list, THAT can cause problems.

8 minutes ago, Nypyren said:

The array only holds references (pointers) to the lists - the list contents are not stored in RAM anywhere near the array.  If your threads are ONLY modifying different Lists, then it will be fine.

If your threads are accessing the cubes array itself and one thread decides to reassign an entry in the array, THAT could cause problems.

If you ever have two threads that access the same list, THAT can cause problems.

Hey, thanks for the reply, yeah the threads will never access same lists and only modify, add or remove contents of a list. I previously had a fixed array size and no lists so I din't have to worry about such things.

Though if you're using Java, it might not be a bad idea to look into this static Collections method (for the internal lists):

Collections.synchronizedList(new ArrayList());

 

3 hours ago, orange451 said:

Though if you're using Java, it might not be a bad idea to look into this static Collections method (for the internal lists):

Collections.synchronizedList(new ArrayList());

 

That adds synchronization overhead on each access to the list, which is not needed if at most one thread accesses it.

 

Edit: Also, it's the wrong programming language you're talking about ?

This topic is closed to new replies.

Advertisement