/** * Schedules the task for execution in currentTick + inXTicks If inXTicks is negative or 0, the * task is not scheduled. * * @param task the task to schedule * @param inXTicks the number of ticks in the future that the task will be scheduled for * execution. * @return true if the task was scheduled. */ public boolean scheduleTask(FrameworkTask task, long inXTicks) { if (task == null) { logger.log(Level.WARNING, "Cannot schedule a null task", currentTick); return false; } if (inXTicks < 1) { logger.log( Level.WARNING, "task {1} was scheduled with inXTicks of {2} but this must be 1 or greater", new Object[] {currentTick, task, inXTicks}); return false; } Long time = currentTick + inXTicks; Set<FrameworkTask> set = taskQueue.get(time); if (set == null) { Set<FrameworkTask> set2 = new ConcurrentHashSet<FrameworkTask>(); set = taskQueue.putIfAbsent(time, set2); if (set == null) { // there wasn't a set already at key 'time' set = set2; synchronized (maxTick) { if (time > maxTick) { maxTick = time; synchronized (lock) { lock.notify(); } } } } } task.setScheduledTick(time); set.add(task); return true; }
/** * Cancels the task from the Task Queue. This is only possible if the tick for which the task is * scheduled has not been reached. * * @param task The task to cancel. * @return true if it was , false otherwise. */ public boolean cancelTask(FrameworkTask task) { if (task != null) { long time = task.getScheduledTick(); if (time > currentTick) { Set<FrameworkTask> set = taskQueue.get(time); if (set != null) { return set.remove(task); } } } else { logger.log(Level.WARNING, "Cannot cancel a null task", currentTick); } return false; }