Search code examples
javamultithreadingthread-safetybitset

Is Java BitSet get() method read only?


I have code which creates an array of size N BitSets. At the start of execution, I do some calculations and then set the bits in the corresponding array indexe to act as a filtering process. For example, if I get given the number 10 and position 100, I check if index 10 has position 100 set to true.

Later in execution, I use multiple threads to call get(bitNum) on particular indexes in the array. There is the possibility of multiple threads accessing the same index in the array, however the BitSet will not be modified in any way. Are these operations thread safe?

I looked at the documentation for BitSet and it states that: A BitSet is not safe for multithreaded use without external synchronization. However, on some SO threads it suggests that they are thread safe for read only operations. Is get() considered a read only operation?

Thanks!


Solution

  • If you can make sure that during the time you are writing to the array no other thread is accessing for reading, then it is safe.

    To read concurrently should not be an issue in my opinion.