Does Java thread sleep puts swing ui to sleep

0 votes

I have the following code where I am trying to run a check at every hour interval on the selected directory(s) using thread.sleep() method through JFileChooser. But the issue is whenever I try to select the directory and execute the method, it puts the UI to sleep as well. Can someone help me out of this?

if (option == JFileChooser.APPROVE_OPTION) {
    selectedDirectory = chooser.getSelectedFiles();
    try {
        while (true) {
            runCheck(selectedDirectory);
            Thread.sleep(1000*5);//1000 is 1 second
        }
    } catch (InterruptedException e1) {
        Thread.currentThread().interrupt();
        e1.printStackTrace();
    }               
} 
Sep 27, 2018 in Java by anto.trigg4
• 3,440 points
5,151 views

2 answers to this question.

0 votes

Your thread.sleep() puts the UI to sleep because it is being invoked on the Event Dispatch Thread which is responsible for running the gui. Thus, the UI stops processing events and 'goes to sleep'.

To avoid this you can make use of javax.swing.Timer.

Timer t = new Timer(1000 * 5, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // do your reoccuring task
    }
});

This won't leave your UI unresponsive.

answered Sep 27, 2018 by geek.erkami
• 2,680 points
0 votes

You are correct about the code putting the UI to sleep. Since sleep is called on the Event Dispatch Thread (the thread responsible for running the gui) the UI stops processing events and 'goes to sleep'.

Timer t = new Timer(1000 * 5, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // do your reoccuring task
    }
});

This will cause your reoccurring task to be performed off of the EDT, and thus it wont leave your ui unresponsive.

answered Oct 24, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
2 answers

Does Java allow to create static classes?

Java has "static nested classes", but they ...READ MORE

answered Sep 5, 2018 in Java by Sushmita
• 6,920 points
1,159 views
0 votes
1 answer

Does python have an equivalent to Java Class.forName()?

This is found in the python standard ...READ MORE

answered Jun 1, 2018 in Java by Parth
• 4,640 points
1,468 views
0 votes
1 answer

How to kill a thread in Java?

You can use a shared variable as ...READ MORE

answered Jun 6, 2018 in Java by Akrati
• 3,190 points
1,212 views
0 votes
1 answer

How to stop the Thread in Java?

Use a flag, which will indicate the ...READ MORE

answered Jun 11, 2018 in Java by Parth
• 4,640 points
1,548 views
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 26, 2018 in Java by developer_1
• 3,350 points
1,253 views
0 votes
2 answers
0 votes
1 answer

Gracefully stopping a java thread

The preferable way will be to use a ...READ MORE

answered Aug 21, 2018 in Java by anto.trigg4
• 3,440 points
1,367 views
+1 vote
1 answer

Multithreading in Java with priorities

HI, @Juan, By default, a thread inherits the priority of its parent thread. ...READ MORE

answered Nov 6, 2020 in Java by Gitika
• 65,770 points
2,572 views
0 votes
2 answers

How to convert ArrayList<String> to String[] in Java

Try this:  String[] strArray = arrayList.toArray(new Str ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
1,021 views
0 votes
2 answers

How to move or copy a file in Java

The new JAVA Specification Request 203 -NIO will ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
994 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP