Fix thread-safety issue

This code was not thread-safe and more recent versions of Ruby raise an exception "RuntimeError: can't add a new key into hash during iteration".

More info: http://jira.codehaus.org/browse/JRUBY-4565