Why need ReaderLock?
Since the readers don't change anything, why we need ReaderLoc开发者_如何学JAVAk?
For one you don't want a writer writing while a reader is reading, because otherwise you may be reading up with corrupted (e.g. half-written) values. (That was mentioned here by other before)
The other important thing in concurrent programming is visibility. If you wouldn't read-lock the VM might cache the old value and you won't see the new one. Sometimes even reordering can be an issue.
A really good book on this topic (and many others) is Java Concurrency in Practice. The free online article Synchronization and the Java Memory Model by Doug Lea also covers it and might be a good start.
There is no such class as ReaderLock
in the standard Java SE class library.
If you are asking about "read locks" in general, the idea is to stop something else acquiring a write lock and writing to the file (or whatever) while we are reading it.
i think you are asking about synchronization. if so , u need to lock before using shared buffer by a reader. if not during reading a writter thread can write the shared buffer and you can read the wrong data. i am not sure whether you are asking this because your question is not clear.
精彩评论