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.