Paul Martin
2016-04-27 c2188a840bc4153ae92112b04b2e06a90d3944aa
src/main/java/com/gitblit/utils/WorkQueue.java
@@ -14,11 +14,6 @@
package com.gitblit.utils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Collection;
@@ -38,7 +33,10 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
/** Delayed execution of tasks using a background thread pool. */
public class WorkQueue {
@@ -53,18 +51,19 @@
  private Executor defaultQueue;
  private final IdGenerator idGenerator;
  private final int defaultQueueSize;
  private final CopyOnWriteArrayList<Executor> queues;
  @Inject
  public WorkQueue(final IdGenerator idGenerator) {
  public WorkQueue(final IdGenerator idGenerator, final int defaultQueueSize) {
    this.idGenerator = idGenerator;
    this.defaultQueueSize = defaultQueueSize;
    this.queues = new CopyOnWriteArrayList<Executor>();
  }
  /** Get the default work queue, for miscellaneous tasks. */
  public synchronized Executor getDefaultQueue() {
    if (defaultQueue == null) {
      defaultQueue = createQueue(1, "WorkQueue");
      defaultQueue = createQueue(defaultQueueSize, "WorkQueue");
    }
    return defaultQueue;
  }
@@ -268,7 +267,8 @@
      return startTime;
    }
    public boolean cancel(boolean mayInterruptIfRunning) {
    @Override
   public boolean cancel(boolean mayInterruptIfRunning) {
      if (task.cancel(mayInterruptIfRunning)) {
        // Tiny abuse of running: if the task needs to know it was
        // canceled (to clean up resources) and it hasn't started
@@ -289,36 +289,44 @@
      }
    }
    public int compareTo(Delayed o) {
    @Override
   public int compareTo(Delayed o) {
      return task.compareTo(o);
    }
    public V get() throws InterruptedException, ExecutionException {
    @Override
   public V get() throws InterruptedException, ExecutionException {
      return task.get();
    }
    public V get(long timeout, TimeUnit unit) throws InterruptedException,
    @Override
   public V get(long timeout, TimeUnit unit) throws InterruptedException,
        ExecutionException, TimeoutException {
      return task.get(timeout, unit);
    }
    public long getDelay(TimeUnit unit) {
    @Override
   public long getDelay(TimeUnit unit) {
      return task.getDelay(unit);
    }
    public boolean isCancelled() {
    @Override
   public boolean isCancelled() {
      return task.isCancelled();
    }
    public boolean isDone() {
    @Override
   public boolean isDone() {
      return task.isDone();
    }
    public boolean isPeriodic() {
    @Override
   public boolean isPeriodic() {
      return task.isPeriodic();
    }
    public void run() {
    @Override
   public void run() {
      if (running.compareAndSet(false, true)) {
        try {
          task.run();