{
private abstract class WorkerManager
{
- static final int IDLE = 0;
- static final int QUEUED = 1;
- static final int RUNNING = 2;
- static final int CANCELLING = 3;
-
- protected volatile int state = IDLE;
protected volatile boolean enabled = true;
protected final AlignCalcWorkerI worker;
this.enabled = enabled;
}
- synchronized protected void setState(int state)
- {
- this.state = state;
- }
-
- int getState()
- {
- return state;
- }
-
- void restart()
+ synchronized void restart()
{
if (!isEnabled())
{
return;
}
- if (state == IDLE)
- {
- submit();
- }
- else if (state == QUEUED)
- {
- // job already queued, do nothing
- }
- else if (state == RUNNING)
+ if (isWorking())
{
cancel();
- submit();
- }
- else if (state == CANCELLING)
- {
- submit();
}
+ submit();
}
+ abstract boolean isWorking();
+
protected abstract void submit();
abstract void cancel();
{
super(worker);
}
+
+ @Override
+ boolean isWorking()
+ {
+ return task != null && !task.isDone();
+ }
@Override
protected void submit()
}
Cache.log.debug(format("Worker %s queued",
worker.getClass().getName()));
- setState(QUEUED);
task = executor.submit(() -> {
- setState(RUNNING);
try
{
Cache.log.debug(format("Worker %s started",
}
finally
{
- // fixme: should not set to idle if another task is already queued for execution
- setState(IDLE);
}
});
}
@Override
synchronized void cancel()
{
- if (task == null || state == IDLE || state == CANCELLING)
+ if (!isWorking())
{
return;
}
Cache.log.debug(format("Cancelling worker %s",
worker.getClass().getName()));
- setState(CANCELLING);
task.cancel(true);
- if (task.isCancelled())
- {
- setState(IDLE);
- }
}
}
this.worker = worker;
}
+ @Override
+ boolean isWorking()
+ {
+ return task != null && !task.isDone();
+ }
+
protected void submit()
{
if (task != null && !(task.isDone() || task.isCancelled()))
}
Cache.log.debug(format("Worker %s queued",
worker.getClass().getName()));
- setState(QUEUED);
final var runnable = new Runnable()
{
private boolean started = false;
{
Cache.log.debug(format("Worker %s started",
worker.getClass().getName()));
- setState(RUNNING);
worker.startUp();
started = true;
}
Cache.log.debug(format("Worker %s finished",
worker.getClass().getName()));
completed = true;
- setState(IDLE);
}
}
} catch (Throwable th)
Cache.log.debug(format("Worker %s failed",
worker.getClass().getName()), th);
completed = true;
- setState(IDLE);
}
if (completed)
{
synchronized protected void cancel()
{
- if (task == null || state == IDLE || state == CANCELLING)
+ if (!isWorking())
{
return;
}
Cache.log.debug(format("Cancelling worker %s",
worker.getClass().getName()));
- setState(CANCELLING);
task.cancel(false);
- if (task.isCancelled())
- {
- setState(IDLE);
- }
executor.submit(() -> {
worker.cancel();
});
}
else
{
- return registered.get(worker).getState() == WorkerManager.RUNNING;
+ return registered.get(worker).isWorking();
}
}
for (var entry : registered.entrySet())
{
if (entry.getKey().involves(annot) &&
- entry.getValue().getState() == WorkerManager.RUNNING)
+ entry.getValue().isWorking())
{
return true;
}
{
for (var manager : registered.values())
{
- if (manager.getState() == WorkerManager.RUNNING)
+ if (manager.isWorking())
{
return true;
}