Zane
Zane

Reputation: 55

Thread.sleep() not working. Operations being skipped

This code is supposed to search the html file of a webpage, print some results to a window, sleep for 60 seconds then search again repeatedly. This worked in python just fine but translating to java is giving me problems. When I try to execute this code, it no longer prints the results but just sleeps indefinitely. Without the while loop, things seem to work as aspected.

btnSearch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            running = true;

            while (running) {

                exportField.setText("Searching...");


                try {

                    exportField.setText(crawler.fetchHtml(url););



                } catch (Exception e) {
                    exportField.setText("invalid parameters.");
                    e.printStackTrace();
                }


                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }


            }

        }
    });

weirder still, if i try something as simple as:

exportField.setText("Searching...");

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {              
    e.printStackTrace();
}

exportField.setText("Done Searching");

I would expect the output to be "searching..." pause "done Searching" but this is not the case. it just outputs "done searching."

Working on this project for fun! Any help would be much much appreciated.

Upvotes: 1

Views: 242

Answers (2)

dbrown93
dbrown93

Reputation: 344

You are starting an infinite loop inside an actionPerformed method as far as I can tell (assuming the running isn't set to false somewhere), this will block all events on the event dispatch thread. Have a look at this tutorial for swing. Long running events like @Jan said should be done with a SwingWorker or on a separate thread at least

Upvotes: 3

Jan
Jan

Reputation: 13858

Seems to me like you are inside of the Event Dispatch Thread. If that were the case, your sleep()s would operate just fine - the repaints on your exportField are just never (in case of while) or delayed (in case of simple sleep) happening.

You should consider looking into SwingWorker for this long-running but UI-changing sort of calls.

A simplified approach would just start a thread and update UI via SwingUtilities.invokeLater():

btnSearch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            new Thread("Fetcher") {

                boolean running = true;

                public void run() {
                    running = true;

                    while (running) {
                        SwingUtilities.invokeLater(() -> exportField.setText("Searching..."));
                        try {
                            exportField.setText(crawler.fetchHtml(url));
                        } catch (Exception e) {
                            SwingUtilities.invokeLater(() -> exportField.setText("invalid parameters."));
                            e.printStackTrace();
                        }
                        try {
                            Thread.sleep(60000);
                        } catch (InterruptedException e) {

                            e.printStackTrace();
                        }
                    }
                }
            }.start();              
        }
    });

Upvotes: 4

Related Questions