protected void execute(ExecutionBuilder.TaskWrapper wrapper) { if (wrapper.synchronizer == null) { executeNow(wrapper); } else { schedule(wrapper); } }
private void executeWaitingTasks() { synchronized (schedulerQueue) { Iterator<ExecutionBuilder.TaskWrapper> iter = schedulerQueue.iterator(); long now = System.currentTimeMillis(); while (iter.hasNext()) { ExecutionBuilder.TaskWrapper wrapper = iter.next(); if (wrapper.waitUntil <= now) { executeNow(wrapper); iter.remove(); } else { // The scheduler queue is sorted by "waitUntil" -> as soon as we discover the // first task which can not run yet, we can abort... return; } } } }
private synchronized void schedule(ExecutionBuilder.TaskWrapper wrapper) { // As tasks often create a loop by calling itself (e.g. BackgroundLoop), we drop // scheduled tasks if the async framework is no longer running, as the tasks would be rejected and // dropped anyway... if (!running) { return; } Long lastInvocation = scheduleTable.get(wrapper.synchronizer); if (lastInvocation == null || (System.currentTimeMillis() - lastInvocation) > wrapper.intervalMinLength) { executeNow(wrapper); } else { if (dropIfAlreadyScheduled(wrapper)) { if (LOG.isFINE()) { LOG.FINE( "Dropping a scheduled task (%s), as for its synchronizer (%s) another task is already scheduled", wrapper.runnable, wrapper.synchronizer); } return; } wrapper.waitUntil = lastInvocation + wrapper.intervalMinLength; addToSchedulerQueue(wrapper); wakeSchedulerLoop(); } }