Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8983089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:44:55+00:00 2026-06-15T20:44:55+00:00

I have two threads. One records the audio data into the variable. Another Thread

  • 0

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);            
        }
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:44:57+00:00Added an answer on June 15, 2026 at 8:44 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has two threads. The first one (the main thread)
I have two threads, a producer thread that places objects into a generic List
If I have two threads and one global variable (one thread constantly loops to
I have app that uses two threads: one thread for regular application work and
I have two threads, one that dispatches messages and another that parses them. Simple,
I have two threads, one reading streams and producing data-objects from their content. I
I have two threads, one thread processes a queue and the other thread adds
I have two threads referencing the same variable -- the UI thread and a
I have two threads. One of them writes to PipedOutputStream, another one reads from
I have two threads in an Android application, one is the view thread, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.