}
}
}
};
while (this.active && !tasks.isEmpty()) {
InstallTask task = null;
synchronized (tasks) {
task = tasks.first();
tasks.remove(task);
}
// async tasks are executed "immediately"
if ( task.isAsynchronousTask() ) {
logger.debug("Executing async task: {}", task);
// set attribute
final Integer oldValue;
if ( task.getResource() != null ) {
oldValue = (Integer)task.getResource().getAttribute(InstallTask.ASYNC_ATTR_NAME);
final Integer newValue;
if ( oldValue == null ) {
newValue = 1;
} else {
newValue = oldValue + 1;
}
task.getResource().setAttribute(InstallTask.ASYNC_ATTR_NAME, newValue);
} else {
oldValue = null;
}
// save new state
this.cleanupInstallableResources();
final InstallTask aSyncTask = task;
final String threadName = "BackgroundTaskThread" + backgroundTaskCounter.incrementAndGet();
final Thread t = new Thread(threadName) {
@Override
public void run() {
logger.debug("Starting background thread {} to execute {}",
Thread.currentThread().getName(),
aSyncTask);
try {
Thread.sleep(2000L);
} catch (final InterruptedException ie) {
// ignore
}
// reset attribute
if ( aSyncTask.getResource() != null ) {
aSyncTask.getResource().setAttribute(InstallTask.ASYNC_ATTR_NAME, oldValue);
}
aSyncTask.execute(ctx);
logger.debug("Background thread {} ends", Thread.currentThread().getName());
}
};
t.start();
return ACTION.SHUTDOWN;