I have been working on a Web Service using Apache Axis2, and trying to keep everything free-threaded.
The service maintains data collection scans, and service requests (each on a separate thread) must interlock with the scan thread, possibly blocking until their "index" in the buffer becomes available.
Needless to say, Thank Deities for the java.util.concurrent package!
Internally, the service maintains a Session for each scan job. There are two important methods here, request() and close(). These are represented by two separate service APIs. Sessions are reference-counted, and the last call actually closes the session, which involves cancelling any in-process scan thread, without interrupting any synchronization going on between it and request() callers.
Of course, how do we make sure close() can operate safely? We have to know that no one is inside of request(), or we will mung up some synchronization somewhere.
This is where inspiration struck! The trick here is the ReaderWriterLock, which has exactly the semantics we need. Callers inside request() obtain the read lock, and callers inside close() obtain the write lock. This keeps everyone organized and off each other's toes.
No comments:
Post a Comment