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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:50:09+00:00 2026-06-09T20:50:09+00:00

I have to send a set of files to several computers through a certain

  • 0

I have to send a set of files to several computers through a certain port. The fact is that, each time that the method that sends the files is called, the destination data (address and port) is calculated. Therefore, using a loop that creates a thread for each method call, and surround the method call with a try-catch statement for a BindException to process the situation of the program trying to use a port which is already in use (different destination addresses may receive the message through the same port) telling the thread to wait some seconds and then restart to retry, and keep trying until the exception is not thrown (the shipping is successfully performed).
I didn’t know why (although I could guess it when I first saw it), Netbeans warned me about that sleeping a Thread object inside a loop is not the best choice. Then I googled a bit for further information and found this link to another stackoverflow post, which looked so interesting (I had never heard of the ThreadPoolExecutor class). I’ve been reading both that link and the API in order to try to improve my program, but I’m not yet pretty sure about how am I supposed to apply that in my program. Could anybody give a helping hand on this please?

EDIT: The important code:

        for (Iterator<String> it = ConnectionsPanel.list.getSelectedValuesList().iterator(); it.hasNext();) {
        final String x = it.next();
        new Thread() {

            @Override
            public void run() {
                ConnectionsPanel.singleAddVideos(x);
            }
        }.start();
    }

    private static void singleAddVideos(String connName) {
    String newVideosInfo = "";

    for (Iterator<Video> it = ConnectionsPanel.videosToSend.iterator(); it.hasNext();) {
        newVideosInfo = newVideosInfo.concat(it.next().toString());
    }

    try {
        MassiveDesktopClient.sendMessage("hi", connName);
        if (MassiveDesktopClient.receiveMessage(connName).matches("hello")) {
            MassiveDesktopClient.sendMessage(newVideosInfo, connName);
        }
    } catch (BindException ex) {
        MassiveDesktopClient.println("Attempted to use a port which is already being used. Waiting and retrying...", new Exception().getStackTrace()[0].getLineNumber());
        try {
            Thread.sleep(MassiveDesktopClient.PORT_BUSY_DELAY_SECONDS * 1000);
        } catch (InterruptedException ex1) {
            JOptionPane.showMessageDialog(null, ex1.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        ConnectionsPanel.singleAddVideos(connName);
        return;
    }

    for (Iterator<Video> it = ConnectionsPanel.videosToSend.iterator(); it.hasNext();) {
        try {
            MassiveDesktopClient.sendFile(it.next().getAttribute("name"), connName);
        } catch (BindException ex) {
            MassiveDesktopClient.println("Attempted to use a port which is already being used. Waiting and retrying...", new Exception().getStackTrace()[0].getLineNumber());
            try {
                Thread.sleep(MassiveDesktopClient.PORT_BUSY_DELAY_SECONDS * 1000);
            } catch (InterruptedException ex1) {
                JOptionPane.showMessageDialog(null, ex1.toString(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            ConnectionsPanel.singleAddVideos(connName);
            return;
        }
    }
}
  • 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-09T20:50:10+00:00Added an answer on June 9, 2026 at 8:50 pm

    Your question is not very clear – I understand that you want to rerun your task until it succeeds (no BindException). To do that, you could:

    • try to run your code without catching the exception
    • capture the exception from the future
    • reschedule the task a bit later if it fails

    A simplified code would be as below – add error messages and refine as needed:

    public static void main(String[] args) throws Exception {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(corePoolSize);
        final String x = "video";
        Callable<Void> yourTask = new Callable<Void>() {
            @Override
            public Void call() throws BindException {
                ConnectionsPanel.singleAddVideos(x);
                return null;
            }
        };
        Future f = scheduler.submit(yourTask);
        boolean added = false; //it will retry until success
                               //you might use an int instead to retry
                               //n times only and avoid the risk of infinite loop
        while (!added) {
            try {
                f.get();
                added = true; //added set to true if no exception caught
            } catch (ExecutionException e) {
                if (e.getCause() instanceof BindException) {
                    scheduler.schedule(yourTask, 3, TimeUnit.SECONDS); //reschedule in 3 seconds
                } else {
                    //another exception was thrown => handle it
                }
            }
        }
    }
    
    public static class ConnectionsPanel {
    
        private static void singleAddVideos(String connName) throws BindException {
            String newVideosInfo = "";
    
            for (Iterator<Video> it = ConnectionsPanel.videosToSend.iterator(); it.hasNext();) {
                newVideosInfo = newVideosInfo.concat(it.next().toString());
            }
    
            MassiveDesktopClient.sendMessage("hi", connName);
            if (MassiveDesktopClient.receiveMessage(connName).matches("hello")) {
                MassiveDesktopClient.sendMessage(newVideosInfo, connName);
            }
    
            for (Iterator<Video> it = ConnectionsPanel.videosToSend.iterator(); it.hasNext();) {
                MassiveDesktopClient.sendFile(it.next().getAttribute("name"), connName);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 16 bits. In each bit I can set some property and send
I'm using twisted. I have my protocols set up so that, to send an
I have a set of 10000 files c1.dat ... c10000.dat. Each of these files
I currently have existing code that automates and email and sends files. I now
I have a set of files I am trying to import into MySQL. Each
I have a script set up to send out multipart emails; plain text and
I have to send three asynch requests in three class files, 3 requests response
I have to send .csv file to remote server through REST in rails. I
I have to send a large amount of xml data through sockets. Example of
I have to send a long string of data through webservice to database.I odnt

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.