Skip to content

Commit

Permalink
[Trial] Seeing if a dedicated thread fixes fixed-size w/ futures
Browse files Browse the repository at this point in the history
  • Loading branch information
Sushisource committed Mar 26, 2024
1 parent 76a5921 commit 84cc7ec
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@

import com.google.common.base.Preconditions;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Semaphore;
import java.util.concurrent.*;

public class FixedSizeSlotSupplier<T> implements SlotSupplier<T> {
private final int numSlots;
private final Semaphore executorSlotsSemaphore;
private final Executor exe;

public FixedSizeSlotSupplier(int numSlots) {
Preconditions.checkArgument(numSlots > 0, "FixedSizeSlotSupplier must have at least one slot");
this.numSlots = numSlots;
executorSlotsSemaphore = new Semaphore(numSlots);
exe = Executors.newFixedThreadPool(1);
}

@Override
Expand All @@ -45,7 +46,8 @@ public CompletableFuture<SlotPermit> reserveSlot(SlotReservationContext ctx) {
throw new RuntimeException(e);
}
return new SlotPermit();
});
},
exe);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ public Optional<SlotPermit> tryReserveSlot(SlotReservationContext ctx) {

@Override
public void markSlotUsed(SlotInfo slotInfo, SlotPermit permit) {
System.out.println("Marking slot used: " + slotInfo + " With permit: " + permit);
inner.markSlotUsed(slotInfo, permit);
usedSlots.put(permit, slotInfo);
}

@Override
public void releaseSlot(SlotReleaseReason reason, SlotPermit permit) {
System.out.println("Releasing slot: " + permit);
inner.releaseSlot(reason, permit);
issuedSlots.decrementAndGet();
usedSlots.remove(permit);
Expand Down

0 comments on commit 84cc7ec

Please sign in to comment.