Вы можете создать тип диспетчера задач, который содержит ссылку на каждую из задач. Этот диспетчер задач может отвечать за запуск каждой задачи и управление службой ExecutorService. Первая и последняя операция каждой задачи - зарегистрировать у менеджера начало и конец задачи. Затем менеджер может построить статистическую картину, которая представляет собой среднее время, затраченное на выполнение каждой задачи.
Диспетчер задач периодически просматривает список выполняемых задач в поисках «выбросов», которые все еще работают и значительно отклоняются от среднего времени, затрачиваемого на выполнение конкретной задачи. Затем он может отменить эти задачи и перезапустить их.
Ниже очень грубый план того, что вы могли бы сделать ...
public class Task implements Runnable {
protected TaskManager manager_ = null;
protected String taskClass_ = null;
protected String taskId_ = null;
protected Task(TaskManager manager, String taskClass) {
manager_ = manager;
taskClass_ = taskClass;
}
/*
* Override this and perform specific task.
*/
protected void perform() { }
public void run() {
try {
manager_.taskStarted(this);
perform();
manager_.taskCompleted(this);
catch(InterruptedException) {
manager_.taskAborted(this);
}
finally {
}
}
}
public class TaskManager {
ExecutorService service_ = null;
public TaskManager() {
service_ = new ExecutorService();
// start the monitoring thread.
service_.execute(this);
}
public void runTask(Task t) {
service_.execute(t);
}
public void taskStarted(Task t) {
1. Note the time that this task (with unique id) has started.
2. Add time to a hash map.
3. Add task to list of executing tasks.
}
public void taskComplete(Task t) {
1. Find the task id in hash map
2. note how long it took to execute.
3. modify statistics of how long the task took against
the task Class Id.
4. Remove task from list of executing tasks.
}
public void taskAborted(Task t) {
// just remove the task from list of running tasks
// without altering the statistics.
}
public void run() {
1. Go though the list of executing tasks looking for
tasks whose current time - start time is outside the
time statistics for the task class id.
2. cancel the task and start again.
}
}