Search code examples
javamultithreadingswinghtmlunitinternet-connection

Check availability of Internet Connection + Communicate between 2 threads


I have 2 questions:

  1. How can I check if Internet connection is whether turned on or turned off? I'm using Html Unit and I'm working on Windows.

  2. I want to make a JLabel that states the availability of internet connection in my JFrame. Something like:

while(true)
{
    if(isOnline()) label.setText("online");
    else label.setText("offline");
}

but I think I need 2 sperated threads, but how could I create these 2 threads and communicate between them and achieve this?


Solution

  • A similar question was answered here: how-to-check-if-internet-connection-is-present-in-java

    To do the second part of your question, I suppose you could call the InetAddress.isReachable method in a java.util.Timer that is wrapped in a SwingWorker thread to periodically poll the connection.