1

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.

orodbhen
  • 2,644
  • 3
  • 20
  • 29
  • This will help you understand the difference about "thread safe" and "access" the data of an array. In short, the arrays are thread safe but you have no guarantee that what you read is the "latest" value set in the array. https://stackoverflow.com/questions/1132507/java-array-thread-safety – anemomylos Aug 02 '17 at 08:57

1 Answers1

1

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.

r0the
  • 617
  • 4
  • 13
  • 1
    Thanks. It appears that [ConcurrentLinkedQueue](https://developer.android.com/reference/java/util/concurrent/ConcurrentLinkedQueue.html) is what I'm looking for. – orodbhen Aug 02 '17 at 09:00