Release locks on crash in Java
I am writing a client-server application using Java-RMI. Some server-side ressources have to be accessed in mutual exclusion (I am using locks for that purpose).
开发者_C百科Now I was wondering what happens when:
- The client calls a remote method on the server
- The remote method acquires a lock for a critical section
- The client crashes before the remote method exits the critical section
Will any locks acquired by the remote method call associated to that client be released? Or will it just be impossible for other clients to acquire the lock afterwards?
Thanks for your answers
What happens is that the remote method keeps executing until it is done, and releases the locks when it exits the critical section. Then it attempts to return the results (if any) to the client, and fails because the connection has been broken.
There is no particular hazard here ...
Of course, if the server is using Lock
objects rather than primitive locks / mutexes, then it needs to do the lock releases in a finally block to deal with the case where it fails due to some unexpected exception. But this is a different issue. The client crashing won't trigger that scenario.
精彩评论