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 26, 2018 in Java by anto.trigg4
• 3,420 points
413 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 26, 2018 by geek.erkami
• 2,320 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,880 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,880 points
53 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 parth295
• 4,620 points
181 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,150 points
78 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 parth295
• 4,620 points
71 views
0 votes
1 answer

difference between wait() and sleep() in java

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

answered Apr 25, 2018 in Java by developer_1
• 3,300 points
61 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 20, 2018 in Java by anto.trigg4
• 3,420 points
37 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered Apr 30, 2018 in Java by sharth
• 3,320 points
94 views
0 votes
2 answers

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

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

answered Aug 21 in Java by Sirajul
• 25,180 points
67 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 in Java by Sirajul
• 25,180 points
33 views