简体   繁体   中英

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);            
        }
    }
});

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

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> .)

处理此问题的一种方法是使用锁存器(如果这是一次性操作)或CyclicBarrier(如果需要重复)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM