public void doTick(int currentTick) { this.currentTick = currentTick; Iterator<PipeTask> iterator = queuedTasks.iterator(); while (iterator.hasNext()) { PipeTask task = iterator.next(); long period = task.getPeriod(); if (period == -2) { iterator.remove(); continue; } if (task.getNextRun() <= currentTick) { runningTasks.put(task.getTaskId(), task); if (period == -1) { iterator.remove(); } else { task.setNextRun(currentTick + task.getPeriod()); } } } for (PipeTask runningTask : runningTasks.values()) { if (runningTask.isSync()) { try { runningTask.run(); } catch (final Throwable throwable) { runningTask .getOwner() .getLogger() .log( Level.WARNING, String.format( "Task #%s for %s generated an exception", runningTask.getTaskId(), runningTask.getOwner().getDescription().getFullName()), throwable); } } else { executor.execute(runningTask); } } runningTasks.clear(); }
@Override public void cancelTask(int taskId) { Iterator<PipeTask> iterator = queuedTasks.iterator(); while (iterator.hasNext()) { PipeTask task = iterator.next(); if (task.getTaskId() == taskId) { task.cancel0(); iterator.remove(); break; } } }
@Override public boolean isQueued(int taskId) { if (taskId <= 0) { return false; } for (PipeTask task : queuedTasks) { if (task.getTaskId() == taskId) { return task.getPeriod() >= -1; } } PipeTask task = runningTasks.get(taskId); return task != null && task.getPeriod() >= -1; }