Search code examples
javarmirpc

Does RMI guarantee the order of execution?


For example, when one client executes something like this:

serverQueue.push(X);
serverQueue.push(Y);
Object Z = serverQueue.front();
boolean orderPreserved = Z.equals(X);

Assuming only one client and that serverQueue is an ordinary queue, is it guaranteed that orderPreserved will always be true?


Solution

  • Does RMI guarantee the order of execution?

    In the sense that you mean yes.

    If thread T makes or attempts to make 2 RMI calls sequentially in a given order to remote object R, then it is guaranteed that R will get those calls sequentially in that same order.

    serverQueue.push(X);
    serverQueue.push(Y);
    Object Z = serverQueue.front();
    

    The semantics of Java method call means that the local (proxy) method calls occur in the source code order. That means that locally the second push doesn't start until the first push returns.

    Now the synchronous nature of RMI means that the local proxy push calls do not return until the response from the remote object comes back. The call on the proxy waits for the response from the remote method call; i.e. either a normal return response (without any value in this case) or an exception response.

    So if you put this all together, you can prove that the 2 push calls on the remote object corresponding to the proxy serverQueue must occur in the same order as the local calls, and that they cannot overlap.

    All you need to establish this is the facts that Java will not reorder sequential events within a thread, and that RMI is synchronous. The rest follows from simple logic, without the need to consider / resort to the details of the RMI implementation.


    Now obviously, if the two push calls were made on different threads, there is NO guarantee that they will be processed in order by the remote object. But that is a different situation to this one.