Skip to content

Instantly share code, notes, and snippets.

View Omertron's full-sized avatar

Stuart Boston Omertron

View GitHub Profile
@kamranzafar
kamranzafar / ThrottledQueue.java
Created January 31, 2012 22:59
A simple throttled blocking-queue that returns elements at a constant rate (M elements in N time)
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
/**
* A simple throttled blocking-queue that returns elements at a constant rate (M
* elements in N time)
*
/**
* Class that throttles requests. Ensures that the StartRequest cannot be called
* more than a given amount of time in any given interval.
*
* StartRequest blocks until this requirement is satisfied.
*
* TryStartRequest returns 0 if the request was cleared, or a non-0 number of
* millisecond to sleep before the next attempt.
*
* Simple usage, 10 requests per second: