RosettaCodeData/Task/Metered-concurrency/Groovy/metered-concurrency-1.groovy
2014-01-17 05:34:36 +00:00

18 lines
368 B
Groovy

class CountingSemaphore {
private int count = 0
private final int max
CountingSemaphore(int max) { this.max = max }
synchronized int acquire() {
while (count >= max) { wait() }
++count
}
synchronized int release() {
if (count) { count--; notifyAll() }
count
}
synchronized int getCount() { count }
}