Reputation: 565
I'm trying to use the ScheduledExecutorService on an application I'm developing, but I'm getting an erratic behavior and can't figure out if I'm doing something wrong or if this is some known issue. I've tried the example on the documentation:
class BeeperControl {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() {
System.out.println("beep");
}
};
final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(
beeper, 10, 10, TimeUnit.SECONDS);
scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
}
}, 60 * 60, TimeUnit.SECONDS);
}
public static void main(String[] args) {
new BeeperControl().beepForAnHour();
}
}
But this only printed the value 'beep' 4 times in 10 minutes, when it should have printed it every 10 seconds. Can someone give me some help?
Kind regards,
Carlos Ferreira
EDIT:
I've added more info to the print instruction and ran the code on 2 different machines, one with Windows XP and another with Unix, look at the results:
UNIX
beep at Mon Oct 17 13:31:34 WEST 2011
beep at Mon Oct 17 13:31:44 WEST 2011
beep at Mon Oct 17 13:31:54 WEST 2011
beep at Mon Oct 17 13:32:04 WEST 2011
beep at Mon Oct 17 13:32:14 WEST 2011
beep at Mon Oct 17 13:32:24 WEST 2011
beep at Mon Oct 17 13:32:34 WEST 2011
Windows XP
beep at Mon Oct 17 13:24:21 BST 2011
beep at Mon Oct 17 13:25:54 BST 2011
beep at Mon Oct 17 13:27:08 BST 2011
beep at Mon Oct 17 13:28:03 BST 2011
beep at Mon Oct 17 13:28:48 BST 2011
beep at Mon Oct 17 13:29:40 BST 2011
beep at Mon Oct 17 13:30:31 BST 2011
Upvotes: 3
Views: 1162
Reputation: 8100
Hava a look at this article. It discusses a very similar problem. This might help in figuring out the real problem.
Upvotes: 0
Reputation: 53674
This article is a good starting point for understanding the problem. basically, windows timers have issues. The jdk ScheduledExecutorService implementation utilizes the "nano time" based APIs in java which are problematic. We had to change our code so that it utilizes java.util.Timer
on windows (which uses the millisecond based APIs and seems to work reliably on windows) and the ScheduledExecutorService everywhere else.
Upvotes: 3
Reputation: 31724
The code works perfectly fine. You might be feeling the other way due to: 1) The API says
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. This answers the behaviour.
2) `scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
}
}, 60*60, TimeUnit.SECONDS);
ie for 1 hour. Hence for about an hour it will display the output. 3) Even though if you change the time from 60*60 to 10, still it wont terminate. Because shutdown is never called.
scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
scheduler.shutdown();
}
}, 10, TimeUnit.SECONDS);
Now it will terminate properly
Upvotes: 1