This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes choice between ReadWriteLock and synchronize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "choice between ReadWriteLock and synchronize" Watch "choice between ReadWriteLock and synchronize" New topic
Author

choice between ReadWriteLock and synchronize

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
In Monhouse's book, in retrieveDvd(long locationInFile) method, the auther choose synchronize (database) instead of using lock.readLock().lock().
The explanation "Using a ReadWriteLock would be a counterproductive in this case, as all operations would need a WriteLock.... would result in poorer performance."

Question:
1. When I decide to use ReadWriteLock instead of synchronize keyword, should I make sure I have a thread doing writing and another thread doing reading?
2. If I only have threads doing reading from a data file, should I use synchronize keyword?
3. If I only have threads doing reading from a data file and I choose to use ReadWriteLock, the lock algorithm will end up checking if there is a write lock exist and slows down the performance. Is it true?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

Again: did you study the API carefully?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
 
subject: choice between ReadWriteLock and synchronize
 
Similar Threads
ReadWriteLocks and Disk Access
Data Locking with a DB File.
NX: Locking and threads
Question from Andrew's book
Threads and database approach question