HobbitOfShire
HobbitOfShire

Reputation: 2414

display delay of update with threading and swing

I have a thread updating my swing interface each time launched.Even though i worked with the invokeLater() method of SwingUtilities class i still have this delay.

This the run of my thread code:

public void run() {
    final int timeout=2000;
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                if (InetAddress.getByName(host).isReachable(timeout)){
                    ServerFrame.listModel.addElement(InetAddress.getByName(host).getHostName() + '\n');
                }
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    try {
        Thread.sleep(10);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Ant this is how i call it

for (int i =63;i<66 ;i++) {
    ping p = new ping("192.168.1."+i);
    p.start();
}

Upvotes: 0

Views: 62

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200168

It looks like your code is defeating its purpose.: from your thread you are submitting code to the main GUI thread which does the network call and is subject to waiting.

You should obtain the result from the network call within your thread code and only then use invokLater to populate the GUI with the result.

Upvotes: 3

Related Questions