Package org.rssowl.core.tests

Source Code of org.rssowl.core.tests.TestJobQueue

/*   **********************************************************************  **
**   Copyright notice                                                       **
**                                                                          **
**   (c) 2005-2006 RSSOwl Development Team                                  **
**   http://www.rssowl.org/                                                 **
**                                                                          **
**   All rights reserved                                                    **
**                                                                          **
**   This program and the accompanying materials are made available under   **
**   the terms of the Eclipse Public License v1.0 which accompanies this    **
**   distribution, and is available at:                                     **
**   http://www.rssowl.org/legal/epl-v10.html                               **
**                                                                          **
**   A copy is found in the file epl-v10.html and important notices to the  **
**   license from the team is found in the textfile LICENSE.txt distributed **
**   in this package.                                                       **
**                                                                          **
**   This copyright notice MUST APPEAR in all copies of the file!           **
**                                                                          **
**   Contributors:                                                          **
**     RSSOwl Development Team - initial API and implementation             **
**                                                                          **
**  **********************************************************************  */

package org.rssowl.core.tests;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.rssowl.core.model.util.ITask;

import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
* @author bpasero
*/
public class TestJobQueue {
  private final int fMaxConcurrentJobs;

  private final AtomicInteger fTotalWork = new AtomicInteger(0);
  private final AtomicLong fDuration = new AtomicLong(0);

  private final AtomicInteger fRunningJobs = new AtomicInteger(0);
  private final AtomicInteger fDone = new AtomicInteger(0);

  /**
   * Creates an instance of <code>JobQueue</code> that allows to add
   * <code>Runnables</code> into a Queue to process them in Jobs up to a
   * certain amount of allowed parallel Jobs.
   *
   * @param maxConcurrentJobs The maximum number of concurrent running Tasks.
   */
  public TestJobQueue(int maxConcurrentJobs) {
    fMaxConcurrentJobs = maxConcurrentJobs;
  }

  /**
   * Adds the given List of Tasks into the Queue. Each Runnable is processed in
   * a <code>Job</code> once the number of parallel processed Tasks is below
   * <code>MAX_SCHEDULED_JOBS</code>.
   *
   * @param tasks The Tasks to add into this Queue.
   */
  public void schedule(final Queue<ITask> tasks) {
    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();
    }
  }

  /**
   * @return The value of done Jobs.
   */
  public int getDone() {
    return fDone.get();
  }

  /**
   * @return The Duration of this Queue.
   */
  public long getDuration() {
    return fDuration.get();
  }
}
TOP

Related Classes of org.rssowl.core.tests.TestJobQueue

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.