1

So im building an application in swing that will ping a url or a list of urls to show the machines status. So if a machine disconnects it will show up as disconnected. I have a list of machines but I need it to loop over forever in intervals so I can know the status of the machine. I know you cant reset the enumeration count so im stuck on a workaround for it.

Currently the part of the code that does the pinging and enumeration is below:

class WorkerThread extends Thread { 
DefaultListModel model;

public WorkerThread(DefaultListModel aModel) { 
    model = aModel; 
} 

public void run() { 
Pinging ping = new Pinging();
int num ;
Enumeration<String> e;


for ( e= model.elements(), num=0; e.hasMoreElements(); num ++){
    String element = e.nextElement();
    for (int i = 0; i<5; i++){
    ping.reachUrl(element);
      //ping.timer(5, e.nextElement()); 
        final int pos = num;
        EventQueue.invokeLater(new Runnable() { 
            public void run() { 
                //ping.reachUrl(e.nextElement());
                String name = ping.getName();
                String addr = ping.getAddr();
                boolean reach = ping.getReach();
                int time = ping.getTime();
                model.set(pos, name+"  " + addr+"  " + reach + "  " + time);
                //model.add(pos, name+"  " + addr+"  " + reach + "  " + time);
                //model.set(pos, "Hello");

            } 
        }); 

        yield(); 
    }



}

} 

}

Adilp
  • 429
  • 1
  • 7
  • 21
  • You don't enum, please see the [scheduleAtFixedRate](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit)) and for an example please see [this question](http://stackoverflow.com/questions/5102519/how-to-use-scheduledexecutorservice-in-java-to-call-a-callable-implementation-at) – mprabhat Nov 28 '14 at 18:20

1 Answers1

3

Use a List rather than an Enumeration.

Maintain an int that records your place in the list, and when you get to the end of the list, start again at the beginning.

But really you want a separate thread for each URL that you're watching. And you certainly don't want to be doing the ping calls on the event thread. Use a SwingWorker, so that it can run in the background and still update the GUI using the done() method when it's finished.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67