Remove unneeded calls to hasScheduledTasks() when fetching from scheduled task queue for event executors.

Motivation:

Currently in the single threaded and global event executors when the scheduled task queue is drained, there is a call to hasScheduledTasks(). If there are scheduled tasks then the the code polls the queue for tasks. The poll method duplicates the exact logic of hasScheduledTasks(). This involves two calls to nanoTime when one seems sufficient.

Modifications:

Directly poll the queue for tasks and break if the task returned is null.

Result:

Should be no noticeable impact on functionality. Two calls to nanoTime have been coarsened into a single call.
This commit is contained in:
Tim Brooks 2016-06-30 13:16:45 -05:00 committed by Norman Maurer
parent 4676a2271c
commit 181c159c24
2 changed files with 13 additions and 21 deletions

View File

@ -104,15 +104,11 @@ public final class GlobalEventExecutor extends AbstractScheduledEventExecutor {
}
private void fetchFromScheduledTaskQueue() {
if (hasScheduledTasks()) {
long nanoTime = AbstractScheduledEventExecutor.nanoTime();
for (;;) {
Runnable scheduledTask = pollScheduledTask(nanoTime);
if (scheduledTask == null) {
break;
}
while (scheduledTask != null) {
taskQueue.add(scheduledTask);
}
scheduledTask = pollScheduledTask(nanoTime);
}
}

View File

@ -276,19 +276,15 @@ public abstract class SingleThreadEventExecutor extends AbstractScheduledEventEx
}
private boolean fetchFromScheduledTaskQueue() {
if (hasScheduledTasks()) {
long nanoTime = AbstractScheduledEventExecutor.nanoTime();
for (;;) {
Runnable scheduledTask = pollScheduledTask(nanoTime);
if (scheduledTask == null) {
break;
}
while (scheduledTask != null) {
if (!taskQueue.offer(scheduledTask)) {
// No space left in the task queue add it back to the scheduledTaskQueue so we pick it up again.
scheduledTaskQueue().add((ScheduledFutureTask<?>) scheduledTask);
return false;
}
}
scheduledTask = pollScheduledTask(nanoTime);
}
return true;
}