There are two aspects to your question.
Assuming sData is a local variable, all you have to do to get two streams to share the array is to declare sData as final . (And if sData is an instance or class variable, you don't even need this ...)
Getting two streams to share an array safely is a little more complicated. The problem is that these two threads need to be synchronized. The send stream must know how much of the array the write stream is writing. In addition, synchronization is necessary to ensure that data recorded by the write stream is visible in the send stream.
In this example, proper synchronization is likely to entail a couple of additional variables to indicate the current positions of the two streams. And since you have to deal with cases where one thread needs to wait for someone to add or send data, you probably have to use Object.wait() and Object.notify() to avoid waiting for a wait.
But what you do on a semantic level looks like it should work like a classic circular buffer of short values. Therefore, I would recommend looking for an existing implementation that you can reuse. (For example, if you are ready to take the short overhead against short , then you can use ArrayBlockingQueue<Short> .)
source share