Search code examples
androidconcurrencythread-safetycircular-buffer

Is Android SDK's CircularArray thread safe?


This question is in regard to android.support.v4.util.CircularArray.

I'm looking at using a CircularArray to feed data from one thread to another. Each element is an array of type short[].

  • Can the Array be safely added to and popped from, concurrently?
  • Can I be sure that when I try to pop an element, that it has been atomically added to the array?
  • Can I safely clear the Array from a particular thread?

The documentation for CircularArray doesn't say anything about thread-safety, but I'm wondering if there are general assumptions specified elsewhere in the SDK documentation that apply to it.


Solution

  • If not explicitly stated, the classes of the Android platform are not thread-safe. So the answer to your questions is no.

    You better use a queue from the java.util.concurrent package to implement your inter-thread communication.