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 6676907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:03:04+00:00 2026-05-26T04:03:04+00:00

I have a android application with lots of buttons. If a button is pressed

  • 0

I have a android application with lots of buttons. If a button is pressed it sends a short cmd to a server via a socket.

Currently, when a button is pressed this adds a cmd to a list.
I have a worker thread that constantly checks the list for cmds and if it finds one opens a socket and sends the cmd.

This is not very efficient as the worker thread is constantly running. What would be the best way to improve this?

public class Arduino implements Runnable{

private static PrintWriter arduinoOutput;
private static Socket ss;
private static Queue<String> cmdsToSend=new LinkedList<String>();
private static String cmd;

public void run(){
    while(true){
        if(!cmdsToSend.isEmpty()){
            cmd = cmdsToSend.poll();
            System.out.println("send:"+cmd);
            if(connect()){
                arduinoOutput.println(cmd);
                disconnect();
            }
        }
    }
}

public static void sendCmd(String newcmd){
    cmdsToSend.add(newcmd);
}

private static boolean connect(){
    try {
        ss = new Socket();
        InetAddress addr = InetAddress.getByName("192.168.1.8");
        int port = 23;
        SocketAddress sockaddr = new InetSocketAddress(addr, port);
        ss.connect(sockaddr, 2000);
        arduinoOutput = new PrintWriter(ss.getOutputStream(),true); //Autoflush
        return true;
    } catch (UnknownHostException e) {
        return false;
    } catch (IOException e) {   
        return false;
    }
}

private static void disconnect(){
    arduinoOutput.close();
    try {
        ss.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

The UI activity adds a cmd by calling Arduino.sendCmd(“cmdName”); The cmds need to be sent as quickly as possible so a sleep in the loop is no good.
Any ideas or examples would be appreciated.

  • 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-05-26T04:03:04+00:00Added an answer on May 26, 2026 at 4:03 am

    Use a wait/notify pattern. Put the sender on a thread with the list. Whenever there is something to write to the worker thread, have the writer add the command, and then notify the thread. If the thread is already awake, the notify will do nothing.

    Here is a quick example, clearly the mechanism you will use to start the writing thread will be different.

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.concurrent.ThreadFactory;
    
    public class Notifier
    {
        public static void main( String args[] )
        {
            Writer writingThread = new Writer();
            writingThread.addToQueue( "Command 0" );
            ThreadFactory.submitInSingleThread( writingThread );
    
            for (int i = 1; i < 1000; i++)
            {
                writingThread.addToQueue( "Command " + i );
                writingThread.notify();
            }
        }
    
        static class Writer implements Runnable
        {
            private static Queue<String> cmdsToSend = new LinkedList<String>();
    
            public void addToQueue( String cmd )
            {
                cmdsToSend.add( cmd );
            }
    
            @Override
            public void run()
            {
                while( true )
                {
                    if( !cmdsToSend.isEmpty() )
                    {
                        String cmd = cmdsToSend.poll();
                        System.out.println( "send:" + cmd );
                        if( connect() )
                        {
                            arduinoOutput.println( cmd );
                            disconnect();
                        }
                    }
    
                    synchronized( this )
                    {
                        wait(); //Can add a timer (100ms, for example)
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application for android and we since we have lots
I have a Web Service and an Android application that uses this web service.
I have android application with ListView, where each row has own refresh button. Refreshing
I have an android application, I'm trying to put a splash screen in via
I have an Android application with version 1.6 I take the wallpaper and show
I have an Android application that connects to Facebook to request authorization of an
I have two threads in an Android application, one is the view thread, and
I have a bunch of EditTexts in my Android application, each with InputMethod set
I have a screen where you can enable/disable modules for my Android application. For
I have an interesting problem being reported to me from an android application I

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.