fTotalWork.set(tasks.size());
final long start = System.currentTimeMillis();
/* Start a new Job for each free Slot */
for (int i = fRunningJobs.get(); i < fMaxConcurrentJobs && tasks.peek() != null; i++) {
Job job = new Job("") { //$NON-NLS-1$
@Override
protected IStatus run(IProgressMonitor monitor) {
final ITask task = tasks.poll();
/* Perform the Operation if not yet Canceld */
try {
task.run(monitor);
} catch (Exception e) {
/* Ignore */
}
/* Inform about cancelation if present */
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return family == TestJobQueue.this;
}
};
/* Listen to Job's Lifecycle */
job.addJobChangeListener(new JobChangeAdapter() {
/* Count the number of running Jobs for the Rule */
@Override
public void running(IJobChangeEvent event) {
fRunningJobs.incrementAndGet();
}
/* Update Fields when a Job is Done */
@Override
public void done(IJobChangeEvent event) {
fRunningJobs.decrementAndGet();
fDone.incrementAndGet();
if (fDone.get() == fTotalWork.get())
fDuration.set(System.currentTimeMillis() - start);
/* Re-Schedule this Job if there is work left to do */
if (tasks.peek() != null)
event.getJob().schedule();
}
});
/* Apply the internal Rule */
job.setSystem(true);
job.schedule();
}
}