G M Ramesh
G M Ramesh

Reputation: 3412

How to Share a Object between two threads (Thread Synchronization)?

I have two threads. One records the audio data into the variable. Another Thread sends that recorded variable to the server. What do I need to do in terms of concurrency, since I am new to multi-threading?

Below is the code snippet:

short[] sData = new short[1024];

recordingThread = new Thread(new Runnable() {
        public void run() {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
            while (isRecording) {
                recorder.read(sData, 0, BufferElements2Rec);
            }

        }
    }, "AudioRecorder Thread");
    recordingThread.start();

and another Thread which is accessing same sData and sending it to the server:

Thread sendThread= new Thread(new Runnable() {
    public void run() {
                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    while (true) {
        try {

      ByteBuffer.wrap(bData).order(ByteOrder.LITTLE_ENDIAN)
            .asShortBuffer().put(sData);            
        }
    }
});

Upvotes: 4

Views: 5415

Answers (3)

Stephen C
Stephen C

Reputation: 718826

There are two aspects to your question.

  • Assuming that sData is a local variable, all you need to do to get the two Threads to share the array is to declare sData as final. (And if sData is an instance or class variable you don't even need to do that ...)

  • Getting the two threads to share the array safely is a bit harder. The problem is that the two threads need to synchronize. The send thread needs to know what part of the array that the record thread has written to. Furthermore, synchronization is necessary to ensure that data written by the record thread is visible to the send thread.

In this example, proper synchronization will most likely entail a couple of extra variables to indicate the current positions of the two threads. And since you have to deal with the cases where one thread needs to wait for the other to add or send data, you probably need to use Object.wait() and Object.notify() to avoid busy waiting.


But what you are doing at the semantic level looks as if it needs to operate like a classical circular buffer of short values. So I'd recommend looking for an existing implementation that you can reuse. (For example, if you were willing to take the overhead of Short versus short then you could use an ArrayBlockingQueue<Short>.)

Upvotes: 2

David Soroko
David Soroko

Reputation: 9086

One way to handle this is to use a latch if this is a one off operation or a CyclicBarrier if this needs to be repeated.

Upvotes: 0

full.stack.ex
full.stack.ex

Reputation: 1747

To minimize reinventing the wheel, you can implement it as producer/consumer synchronization.For starters:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ArrayBlockingQueue.html

Just in case, some introductory stuff is here:

http://en.wikipedia.org/wiki/Producer-consumer_problem

Upvotes: 3

Related Questions