Reputation: 1326
I'm thinking of implementing something that closely resembles the leaky bucket algorithm using the Java Semaphore
class and was wondering whether it would be a good fit. The goal is to limit the rate of writes to a shared resource, and I would have one thread periodically releasing a number of permits to the semaphore, and a pool of worker threads attempting to acquire as many permits as the size of the item they want to write.
My concern is whether the Semaphore
is implemented using a single int
behind the scenes, or if its space usage is linear in the number of active permits (implemented using some sort of queue of permits or such.) If the space (and hence time) is linear, then I'd obviously want to avoid talking about rates in bytes. If it's just an int
, I should have no such concern other than overflow for very high rates (in which case I'd want a long
-backed Semaphore
)
Anyone have any ideas?
Upvotes: 3
Views: 2629
Reputation: 2104
From http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html:
no actual permit objects are used; the Semaphore just keeps a count of the number available and acts accordingly.
I checked the source, and it is indeed backed by an int
(not a long
).
Upvotes: 3