multithreadingjruby

JRuby thread safety and arrays


The official document about JRuby thread safety states:

At least these classes are not considered thread-safe, and if you intend to mutate them concurrently with other operations you will want to introduce locking (e.g. with Mutex): String, Array, Hash, and any data structures derived from them.

What exactly is the consequence of this, in particular the statement that the classes themselves are not threadsafe? Does it mean that if I use some Array in one thread, and I use a completely different Array in a different thread, I get a possible concurrency problem? Or does it mean that if I monkeypatch the Array class in one thread, and I do the same in a different thread, I could get a problem?


Solution

  • Does it mean that if I use some Array in one thread, and I use a completely different Array in a different thread, I get a possible concurrency problem?

    No

    Or does it mean that if I monkeypatch the Array class in one thread, and I do the same in a different thread, I could get a problem?

    That would be an issue but the guide is not about monkey-patching.

    The problem is you can not get predictable behavior when an Array instance is shared between multiple threads.

    To clarify further if you create an Array in one thread and than multiple threads simply read it that is okay. Issues arise when mutating an Array instance while other threads would read/write to the same instance.

    Same applies for Hash and String.