Hack Forums
[Java] Sleep / Wait - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Sleep / Wait (/showthread.php?tid=5538062)



Sleep / Wait - Mohinder - Jan 30, 2017

Hi guys,

I've been reading a lot on how to let the program sleep for X seconds/minutes.

For example;

I want to create a loop that checks a specific condition for every X minutes.
What is the best way to do this?


* - Stealthâ„¢ - Jan 30, 2017

You could use a ScheduledExecutorService to accomplish this.


* - g4143 - Jan 30, 2017

(Jan 30, 2017 05:21 AM)Mohinder Wrote:  Hi guys,

I've been reading a lot on how to let the program sleep for X seconds/minutes.

For example;

I want to create a loop that checks a specific condition for every X minutes.
What is the best way to do this?

It really depends on the specific condition you are waiting for. If its IO then there are plenty of solutions which allow a program to wait/timeout without consuming resources.


* - Psifour - Jan 30, 2017

(Jan 30, 2017 06:50 AM)g4143 Wrote:  It really depends on the specific condition you are waiting for. If its IO then there are plenty of solutions which allow a program to wait/timeout without consuming resources.

This.

We are unable to adequately answer your question without more information. If you can provide a bit more detail I am sure that we can get you an optimal solution.


* - Mohinder - Jan 31, 2017

I should have tell you guys some more, excuse me.
I do have a while loop in my main thread thats waiting for a command. The loop crawls a page and look for any new command. I don't want the loop to check every second, because that will cause the page to be disappointed with the crawler.

Code
while (1 < 2)
        {
            if (!cmds.contains(data)) {
                cmds.add(data);

            }
            try {
                TimeUnit.SECONDS.sleep(waitingTime);

            } catch (InterruptedException ex) {
                
            }
        }

I'm now doing it this way, but I don't know how to do this properly.


* - Stealthâ„¢ - Jan 31, 2017

(Jan 31, 2017 02:52 AM)Mohinder Wrote:  I should have tell you guys some more, excuse me.
I do have a while loop in my main thread thats waiting for a command. The loop crawls a page and look for any new command. I don't want the loop to check every second, because that will cause the page to be disappointed with the crawler.

Code
while (1 < 2)
        {
            if (!cmds.contains(data)) {
                cmds.add(data);

            }
            try {
                TimeUnit.SECONDS.sleep(waitingTime);

            } catch (InterruptedException ex) {
                
            }
        }

I'm now doing it this way, but I don't know how to do this properly.

Like I said, use a ScheduledExecutorService.


* - Mohinder - Jan 31, 2017

(Jan 31, 2017 08:06 AM)Stealthâ„¢ Wrote:  Like I said, use a ScheduledExecutorService.

I will try that out, I will post the results here.

EDIT:

I've tryed it out and it works perfect man. A lot of thanks!

Code
Runnable controleren = new Runnable() {
            public void run() {
                if (!cmds.contains(data)) {
                    cmds.add(data);
                }
            }
        };

        ScheduledExecutorService loop = Executors.newSingleThreadScheduledExecutor();
        loop.scheduleAtFixedRate(controleren, 0, 5, TimeUnit.SECONDS);