/*
* Created by Andrey Cherkashin (acherkashin)
* http://acherkashin.me
*
* License
* Copyright (c) 2015 Andrey Cherkashin
* The project released under the MIT license: http://opensource.org/licenses/MIT
*/
package ragefist.core;
import java.util.LinkedList;
/**
*
* @author acherkashin
*/
public class Processor
{
private final LinkedList<ProcessorTask> _tasksQueue = new LinkedList<>();
private final ProcessorStatistic _statistic = new ProcessorStatistic();
public static class ProcessorStatistic {
private int _maxNewTasksQueue = 0;
private int _sumTasks = 0;
private long _sumTasksExecutionTime;
public final int getMaxNewTasksQueue() { return _maxNewTasksQueue; }
public final int getSumTasks() { return _sumTasks; }
public final float getAverateExecutionTime() { return _sumTasks == 0 ? 0 : ((float)_sumTasksExecutionTime / 1000.0f / (float)_sumTasks); }
}
// ---------------------------------------------------------------------- //
// PUBLIC
// ---------------------------------------------------------------------- //
public ProcessorStatistic getStatistic() {
return _statistic;
}
public void addTask(ProcessorTask task) {
synchronized(_tasksQueue) {
_tasksQueue.add(task);
}
}
public int getTasksCount() {
synchronized(_tasksQueue) {
return _tasksQueue.size();
}
}
public int processTasks() {
synchronized(_tasksQueue) {
// statistic
_statistic._maxNewTasksQueue = Math.max(_statistic._maxNewTasksQueue, _tasksQueue.size());
// processing
while(_tasksQueue.isEmpty() == false) {
_statistic._sumTasks++;
_tasksQueue.poll().execute(this);
}
}
return 1;
}
}