Skip to content

Instantly share code, notes, and snippets.

@kamatama41
Created February 7, 2016 05:06
Show Gist options
  • Save kamatama41/14b74ed50a6294853d4c to your computer and use it in GitHub Desktop.
Save kamatama41/14b74ed50a6294853d4c to your computer and use it in GitHub Desktop.
Try to implement java.util.concurrent.CountDownLatch in Ruby
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