views:

240

answers:

6

Consider the following code:

while(true) { someFunction(); Thread.sleep(1000); }

What I want is that, someFunction() be called once every 10 seconds. But this is not the case. It is being called every second. I tried Thread.wait(1000), but even that doesnt help. I removed of the while part, just kept the body, and at the end wrote :

Thread.start();

But it throwed an exception. Is there any other solution to this?

+11  A: 

It's being called every second because you're sleeping for 1000 milliseconds, aka 1 second.

Change it to Thread.sleep(10000) and that'll be better for you.

Alternatively, use

Thread.sleep(TimeUnit.SECONDS.toMillis(10));

which means you don't have to do the arithmetic yourself. (Many APIs now take a quantity and a TimeUnit, but there doesn't appear to be anything like that for Thread.sleep unfortunately.)

Note that this will make the thread unresponsive for 10 seconds, with no clean way of telling it to wake up (e.g. because you want to shut it down). I generally prefer to use wait() so that I can pulse the same monitor from a different thread to indicate "I want you to wake up now!" This is usually from within a while loop of the form

while (!shouldStop())

EDIT: tvanfosson's solution of using a Timer is also good - and another alternative is to use ScheduledExecutorService which can be a bit more flexible (and easier to test).

Jon Skeet
@Jon, are we sure that mithun1538 is using ThreadPool at all?
The Elite Gentleman
ThreadPool? I havent heard that term before. I am developing a simple application, so I am guessing that I am not using "ThreadPool".
mithun1538
+3  A: 

Thread.sleep() takes the number of miliseconds to sleep. Thus, calling Thread.sleep(1000) sleeps 1000 miliseconds, which is 1 second. Make that Thread.sleep(10000) and it will sleep 10 seconds.

Thomas Lötzer
+1  A: 

What you probably want to do is use a Timer and set up a scheduled invocation of the function rather than sleeping. If you need more exactness with regard to the interval, you can keep track of how long it has been since the last run and adjust the new timer to keep your interval from drifting. See an example of a simple timer in this tutorial.

I won't go into detail about the issue with regard to the precision of the argument, since @Jon has already covered that -- you need milliseconds rather than seconds.

tvanfosson
A: 

What I want is that, someFunction() be called once every 10 seconds.

Then why do you call Thread.sleep() with 1000 as parameter? That's in milliseconds, so you're explicitly saying "wait 1 second".

Michael Borgwardt
A: 

1 second is 1000 milliseconds, so if you want 10 seconds = 10 * 1000 = 10000 milliseconds

e.g.

try { 
     long numMillisecondsToSleep = 5000; // 5 seconds 
     Thread.sleep(numMillisecondsToSleep); 
} catch (InterruptedException e) { } 
The Elite Gentleman
A: 

Consider 1000 milliseconds is 1 second. For that you should use Thread.sleep(10000) for acheiving pause your thread in 10 seconds. You can also use looping with how many seconds to wait your thread. Ex. suppose you want to pause your thread in half-an-hour( 30 minutes). Then use,

for(i=0;i<1800;i++)
{
   Thread.sleep(1000);
}

It will pause your thread in 30 minutes.

Venkats