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;
        }
    }
}
Community
  • 1
  • 1
  • 2
    Instead of talking about it, you should post the code itself. – Marko Topolnik Aug 16 '12 at 18:45
  • I'll +1 you if you change your username to something real then you'll have a rep of 21 which should be enough to access stackoverflow chat (up on the top next to the review link). Because what your asking is not a question. – Adam Gent Aug 16 '12 at 19:30
  • I thought name changing possibility required minimum reputation, sorry dude. @MarkoTopolnik, code added. Thanks for your interest (I though an explanation would be preferred rather than the code). – Jorge Antonio Díaz-Benito Aug 16 '12 at 20:11
  • Your question is still very unclear. A wall of text very hard to follow and a piece of code that clarifies very little. Still "not a real question". – Marko Topolnik Aug 16 '12 at 20:18
  • Basically, my problem is how to tell the tpe to run the tasks not for a given time, but until they're properly finished (which means they don't throw the BindException). Because telling the constructor a time limit for each thread, even if set to Integer.MAX_VALUE, will cause a problem since almost always there will be necessary sending the stuff to two or more different destinations through the same ports and, if the stuff to send consists in many files, each shipping can take more than the said time, and therefore the other shippings through those ports would be skipped. – Jorge Antonio Díaz-Benito Aug 17 '12 at 08:08

1 Answers1

1

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);
        }
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thanks for your help, but this makes no improvement to my way of doing it. In this case, if the BindException is thrown, what the program does is wait and repeat (same as my program does). What I expected to achieve with this new-for-me classes (sorry if it wasn't 100% clear, my fault) was something like: "I'm a thread in the queue that has to use the port 33333, but when I've tried to use it I've obtained a BindException because another one is using it, so let's wait until I get warned that the port is free, and then I may retry (and that once and again until I get to bind the port)." – Jorge Antonio Díaz-Benito Aug 17 '12 at 11:01
  • @Stoyicker So the difference is that instead of retrying every x seconds until it works, you want to use some sort of listener/callback that will let you know when the port is ready to use and run when that happens? – assylias Aug 17 '12 at 11:04
  • Yeah, because in that case I would be able to minimize the time that the task have to be in the queue but at the same time making sure that no task is thrown away from it without having successfully finished. – Jorge Antonio Díaz-Benito Aug 17 '12 at 11:18
  • You could use a CompletionService - check [this similar question](http://stackoverflow.com/questions/11998519/best-way-to-wait-on-tasks-to-complete-before-adding-new-ones-to-threadpool-in-ja). – assylias Aug 17 '12 at 11:21
  • Pretty good to know about that class, thanks. The linked question and [this link](http://blog.teamlazerbeez.com/2009/04/29/java-completionservice/) have allowed me to solve my issue. – Jorge Antonio Díaz-Benito Aug 17 '12 at 15:55