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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:58:27+00:00 2026-05-25T11:58:27+00:00

I have a small bit of code that runs in an applet that contains

  • 0

I have a small bit of code that runs in an applet that contains SWING controls and is used to write information to a socket on a certain port and then listens for a response. This works fine, but there is a problem with it. The port listener is essentially in a loop until null is received by the server. I want users to be able to perform other actions in the GUI instantiated by the applet while waiting for the server to respond (this could take minutes to occur). I also need to worry about the connection between the server and the client disconnecting. But the way the code is written, the applet appears to freeze (its really in a loop) until the server responds. How can I allow the listener to do its listening in the background, allowing other things to occur in the program. I assume I need to use threads and I’m sure for this application, it is easy to implement, but my lack of a solid thread foundation is hampering me. Below is the code (you can see how simple it is). How can I improve it to make it do what I need it to do>

  public String writePacket(String packet) {
/* This method writes the packet to the port - established earlier */
   System.out.println("writing out this packet->"+packet+"<-");
   out.println(packet);
  String thePacket =    readPacket();  //where the port listener is invoked.  
   return thePacket;
   }
    private String readPacket() {
      String thePacket ="";
      String fromServer="";
      //Below is the loop that freezes everything.   
     try {
      while ((fromServer = in.readLine()) != null) { 
        if (thePacket.equals("")) thePacket = fromServer;
        else 
        thePacket = thePacket+newLine+fromServer;
    }
        return thePacket;  //when this happens, all listening should stop.   
   } catch (IOException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
    }

     }  

Thanks,

Elliott

  • 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-25T11:58:28+00:00Added an answer on May 25, 2026 at 11:58 am

    There lots of different means of getting the IO performed on a different thread, but in this case you probably want to use SwingWorker.

    Your code would look something like:

    private final Executor executor = Executors.newSingleThreadExecutor();
    
    public void writePacket(final String packet) 
    {
      // schedules execution on the single thread of the executor (so only one background operation can happen at once)
      //
      executor.execute(new SwingWorker<String, Void>()
          {
    
            @Override
            protected String doInBackground() throws Exception
            {
              // called on a background thread
    
    
              /* This method writes the packet to the port - established earlier */
              System.out.println("writing out this packet->"+packet+"<-");
              System.out.println(packet);
              String thePacket = readPacket();  //where the port listener is invoked.  
              return thePacket;            
            }
    
            @Override
            protected void done()
            {
              // called on the Swing event dispatch thread
    
    
              try
              {
                final String thePacket = get();
    
                // update GUI with 'thePacket'
              }
              catch (final InterruptedException e)
              {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              catch (final ExecutionException e)
              {
                // TODO Auto-generated catch block
                e.printStackTrace();
              } 
            }
          });
    }
    
    private String readPacket() 
    {
      String thePacket ="";
      String fromServer="";
      //Below is the loop that freezes everything.   
      try 
      {
        while ((fromServer = in.readLine()) != null) 
        { 
          if (thePacket.equals("")) 
            thePacket = fromServer;
          else 
            thePacket = thePacket+newLine+fromServer;
        }
        return thePacket;  //when this happens, all listening should stop.   
      } 
      catch (IOException e) 
      {
        e.printStackTrace();
        return null;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have this small bit of code that generates rows and columns of
I have a small bit of code that I'm trying to execute with the
Quick question I currently have a small bit of code that is pulling comments
Using Rails 3.0, I have a small bit of code that I seem to
I have a small piece of code that requires to read 4-bit values bitpacked
I have a small bit of code that gets my images from smugmug. My
So, I have a small bit of javascript code that makes a popup div.
I used to have a small chat app(which was almost working), that uses PHP,
I have a bit of JavaScript code that is specified in a configuration file
I am converting a some C++ to java and have a small bit that

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.