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

  • SEARCH
  • Home
  • 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 7824371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:34:28+00:00 2026-06-02T08:34:28+00:00

I am working with an app which uses two threads in order to read

  • 0

I am working with an app which uses two threads in order to read and send data to server. Each thread runs a while loop; a que holds a que of commands which are dequed to send to the server. This way I can line up a large number of commands which are dequed and sent to the server one by one.

The command sender is as such

 class writeThread extends AsyncTask<Object, Object, Object>
 {
    byte[] buffer;
    LittleEndianDataOutputStream outputStream;
    @Override
    protected void onPreExecute() 
    {

    }
    @Override
    protected Object doInBackground(Object... params) 
    {
        try
        {
            buffer = new byte[4096];
            outputStream = new LittleEndianDataOutputStream(dataHolder.connection.getOutputStream());

            try
            {
                dataHolder.flags latestFlag;
                while (true)
                {
                    try 
                    {
                        latestFlag = dataHolder.sendFlags.remove();
                        ByteBuffer sendBytes = ByteBuffer.allocate(128);
                        sendBytes.order(ByteOrder.LITTLE_ENDIAN);
                        switch (latestFlag) 
                        {
                            case sendDataRequest:
                                sendBytes.putInt(20);
                                outputStream.write(sendBytes.array());
                                break;
                            case getSelectedData:
                                sendBytes.putInt(21);
                                sendBytes.put(dataHolder.latestSelected.getBytes());
                                outputStream.write(sendBytes.array());
                                break;
                            case disconnect:
                                sendBytes.putInt(254);
                                sendBytes.put(dataHolder.latestSelected.getBytes());
                                outputStream.write(sendBytes.array());
                                break;                                    

                        }


                    }
                    catch (NoSuchElementException ex){}
                }
            }
            catch (IOException ex) {}

        }
        catch (Exception ex){};
        return null;

    }


}

The read loop looks as such

     while (inputStream.read() > -1)

As a result, the app ends up taking up 50% of processing power of the phone. Are there any suggestions on how to optimize socket listening and data sending?

Note: Yes I know I am using a try catch for the queue instead of and if statement to check if the queue not empty. I am sure that the error throwing is not helping the optimization but I don’t think it would push the processor to 50%. Let me know if you need anymore info.

  • 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-02T08:34:30+00:00Added an answer on June 2, 2026 at 8:34 am

    The problem with your implementation is that you are basically doing a busy wait – if there is nothing to send, it immediate goes to the exception handler and tries again – thus taking up all the cpu on that core.

    What you want instead is a way to wait until an event is available. I suggest that you create a HandlerThread, and use a Handler that is associated with that thread to handle writing to the data stream.

    HandlerThread handlerThread = new HandlerThread("blah");
    handlerThread.start();
    Handler handler = new Handler(handlerThread.getLooper()) {
        public boolean HandleMessage(Message msg) {
            switch (msg.what) {
                case sendDataRequest:
                   ...
                case getSelectedData:
                   ...
                case disconnect:
                   ...
            }
        }
    }
    

    And then you would create messages with the appropriate id and data, and send them to the handler with handler.sendMessage()

    Alternately (and perhaps more simply), if you made your dataHolder.sendFlags collection an ArrayBlockingQueue, rather than whatever it is currently (I’m guessing ArrayList or similar?), then, when you go to remove the next element with ArrayBlockingQueue.take(), it will wait until something is available (without performing a busy wait that takes up all the available cpu time)

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

Sidebar

Related Questions

I’m working on a Java server app which uses Spring 3 and C3P0 to
I am working on an app which uses Commonslang.jar libraries. I was trying to
I used to have a small chat app(which was almost working), that uses PHP,
I am working on an application which uses a UITabBarViewController to hold two UITableViewControllers.
I am working on an app which uses viewpager with around 100-150 views in
I have an Library-style app which uses Core Data to store its data. Currently
I am working on an application which uses tomcat as a web server and
My web app uses half a dozen tables, each of which get populated when
I am working on an app which uses a UIScrollView to scroll through a
I'm working on a TV Guide app which uses a ListActivity showing the TV

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.