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
Merge pull request #165 from juniorz/thread_safety

Fix thread-safety issue