Created
February 7, 2016 05:06
-
-
Save kamatama41/14b74ed50a6294853d4c to your computer and use it in GitHub Desktop.
Try to implement java.util.concurrent.CountDownLatch in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CountDownLatch | |
def initialize(count) | |
raise ArgumentError, 'count < 0' if count < 0 | |
@count = count | |
@lock = Monitor.new | |
@condition = @lock.new_cond | |
end | |
def count_down | |
@lock.synchronize do | |
@count -= 1 if @count > 0 | |
@condition.broadcast if @count == 0 | |
end | |
end | |
def await | |
@lock.synchronize do | |
@condition.wait if @count > 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment