java.util.concurent - scheduleAtFixedRate() high cpu usage
Radu Rendec
radu.rendec@ines.ro
Wed Jan 9 23:26:00 GMT 2008
Hi,
While trying to migrate one of the applications that I develop from Sun
jdk to gcj, I ran accross a strange problem with scheduleAtFixedRate()
method from ScheduledExecutorService.
Although tasks are scheduled at 60 seconds, the scheduler seems to do
some kind of busy waiting (cpu usage goes to approx. 100%). Running
strace on gij threads revealed that one of the threads is doing
intensive clock_gettime() calls:
gettimeofday({1199901706, 870292}, NULL) = 0
clock_gettime(CLOCK_REALTIME, {1199901706, 870319892}) = 0
These two calls are made continuously, probably causing the ~100% cpu
usage. My guess is that the cpu "greedy" thread is exacly the scheduler
thread, and it's continuously polling the system clock to determine when
to run the task.
My gcc version is 4.1.2 - if this is a known issue (and maybe is already
fixed in later versions), then I apologize for the trouble. However, any
feedback on this issue is welcome. Code to reproduce the problem can be
found below. If you reply, please cc me (I'm not subscribed to the
list).
Thanks,
Radu Rendec
// Main.java
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;
public class Main {
public static void main(String[] args) {
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
Runnable taskGenerator = new MyTask();
final ScheduledFuture<?> taskGeneratorHandle =
scheduler.scheduleAtFixedRate(taskGenerator, 0, 60, SECONDS);
}
}
// MyTask.java
public class MyTask implements Runnable {
public void run() {
System.out.println(System.currentTimeMillis());
}
}
More information about the Java
mailing list