0

So I am developing a javafx application that does a simple job of pinging servers, retrieving their results and displaying them in the UI. For this cause I have developed a javafx UI for displaying the status of the ping requests and another java class that actually pings the server.

All of this was handled using the javafx UI thread which caused my application to hang when the ping requests were sent. So i did some research and found out that the back ground tasks need to be executed in a different thread to stop the UI from hanging.

So I went ahead and used the executor service in the concurrency framework that lets me delegate the ping task to a separate thread.

The problem now is that when i call the get method of the executor service from my main thread the UI still hangs. I was wondering if there is some way of actually displaying a progress bar or status bar when the back ground thread executes. I have tried isdone() which also does not work. Invoking isdone() always gives me a false value.

Here is my code

This is the code snippet from the UI class that calls up the back ground thread to ping the server

Future result = executorService.submit(new Get_Server_Status(serverList.get(i)));

try {
    // the application hangs for 10 seconds here                    
    String code = result.get().toString();          
} catch (InterruptedException e) {              
    e.printStackTrace();                
} catch (ExecutionException e) {
    e.printStackTrace();
}

This is the java class that does the computation

public class Get_Server_Status implements Callable<String> {    
    String URL;
    int code;
    String responseCode;

    public Get_Server_Status(String url) {
        this.URL = url;
    }

    @Override
    public String call() throws Exception {
        String result = "";
        try {
            URL siteURL = new URL(URL);
            System.out.println("New thread is " + Thread.currentThread().getName());
            HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
            connection.setRequestMethod("GET");
            System.out.println("This"+" "+URL+" "+"is being executed");
            connection.setConnectTimeout(1000);
            connection.connect();
            code = connection.getResponseCode();
            responseCode = Integer.toString(code);
        } catch(Exception e) {
            result = "-> Red <-\t" + "Wrong domain - Exception: " + e.getMessage();
            responseCode = "Server Unavailable";
        }
        return responseCode;
    }
}

The get response from this class makes my UI hang. Larger the number of servers to be pinged larger is the UI unresponsiveness.

Can anyone help me out regarding what the best solution to this would be? Should I show a progress bar or can the UI unresponsiveness be reduced in some way?

Any help you can provide would be greatly appreciated.

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Rahul Nair
  • 15
  • 4
  • See if some ideas from [here](https://stackoverflow.com/questions/51955550/remove-tableview-entries-when-status-change) can help. – SedJ601 Dec 13 '19 at 16:50
  • 2
    `Future.get()` blocks until the result is available to just the same effect as executing the logic on the same thread + some overhead for creating a new thread & synchronisation... Use a `Task` or `Platfrom.runLater`... – fabian Dec 13 '19 at 17:47
  • Related: [Properly doing multithreading and thread pools with JavaFX Tasks](https://stackoverflow.com/questions/29733004/properly-doing-multithreading-and-thread-pools-with-javafx-tasks). Note, that example uses a table to monitor and manage concurrent tasks. If you don't need such functionality, you can replace the table with a simpler UI, but the task based handling logic remains the same. – jewelsea Dec 13 '19 at 21:57

0 Answers0