Search code examples
javanetwork-programmingmethodsrmi

Threads and RMI Objects


In Java RMI, is a new thread started for each new Remote Object that is created?

If so, is this thread a sort of listener thread, who serves incoming invocations by creating a new thread for each method invocation?

I just came up with this mental model, and I 'm looking for validation.


Solution

  • The RMI specification neither prescribes nor denies any threading model. When programming the remote part of the program (that which runs in the server) you should assume that the very same object could be accessed by two or more threads at the same time, so you need to synchronize access to mutable fields (even if I think most implementations serialize calls)

    Quote from the spec

    A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. The RMI runtime makes no guarantees with respect to mapping remote object invocations to threads. Since remote method invocation on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread-safe.