Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Sleep / Wait

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?
You could use a ScheduledExecutorService to accomplish this.
(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.
(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.
I don't know how to Java.
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.
(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.
(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);


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by SIM-2362662Nov 10, 2015 11:48 AM 10 504 Nov 10, 2015 11:48 AM
Last Post: IAmNotHere
[Shell] by ๖ۣۜLogic_BombNov 7, 2011 11:18 AM 3 136 Nov 7, 2011 11:18 AM
Last Post: jessman420
[.NET] by SIM-524734Sep 7, 2011 08:09 AM 10 267 Sep 7, 2011 08:09 AM
Last Post: Coleak