Replace Semaphore with AtomicInteger Since we are only utilizing the non-blocking operations of Semaphore, Its better to use AtomicInteger to avoid all the extra overhead. Change-Id: I219b3b35468fe11ef5bf063f671b5f6415630a0c
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotaWithPermits.java b/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotaWithPermits.java index 027ddcf..ea414ca 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotaWithPermits.java +++ b/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotaWithPermits.java
@@ -1,20 +1,25 @@ package com.googlesource.gerrit.plugins.quota; import com.google.gerrit.server.git.WorkQueue; -import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; public abstract class TaskQuotaWithPermits implements TaskQuota { - protected final Semaphore permits; + protected final AtomicInteger permits; public TaskQuotaWithPermits(int maxPermits) { - this.permits = new Semaphore(maxPermits); + this.permits = new AtomicInteger(maxPermits); } public boolean isReadyToStart(WorkQueue.Task<?> task) { - return permits.tryAcquire(); + if (permits.decrementAndGet() >= 0) { + return true; + } + + permits.incrementAndGet(); + return false; } public void onStop(WorkQueue.Task<?> task) { - permits.release(); + permits.incrementAndGet(); } }