Thursday, February 22, 2007

Is RMI Thread Safe?

When the thread safety question extends to the remote calls, e.g. RMI, it becomes even more interesting.

According to the specification defined with Java 1.4.2, RMI section 3.2,
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.
No matter how confusing this specification is, it at least tells us two things,
  1. RMI does not take care of the thread safety, it leaves the thread safety as it is.
  2. RMI does not give you one server thread for one client thread, actually, it guarantees nothing on the thread scheduling.
It sounds a little bit discouraging, but it is good enough already since it does not break the thread safety either.

As long as both the client objects and the server objects are thread-safe, the application is thread-safe.

No comments: