Skip to content

Instantly share code, notes, and snippets.

@vemacs
Created November 23, 2016 04:09
Show Gist options
  • Save vemacs/3f2555cacfa5dbb8823a228c37ef122e to your computer and use it in GitHub Desktop.
Save vemacs/3f2555cacfa5dbb8823a228c37ef122e to your computer and use it in GitHub Desktop.
protected final Queue<FutureTask<?>> j = new java.util.concurrent.ConcurrentLinkedQueue<FutureTask<?>>() {
// PaperSpigot: Make size() constant-time
private AtomicInteger cachedSize = new AtomicInteger(0);
@Override
public boolean add(FutureTask<?> e) {
boolean result = super.add(e);
if (result) {
cachedSize.incrementAndGet();
}
return result;
}
@Override
public FutureTask<?> poll() {
FutureTask<?> result = super.poll();
if (result != null) {
cachedSize.decrementAndGet();
}
return result;
}
@Override
public int size() {
return cachedSize.get();
}
}; // Spigot, PAIL: Rename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment