Concurrency
Concurrency
- When multiple threads access the same resources, care must be taken to
ensure that data is accessed in a consistent manner.
- Example: Bank account transfer, academic record
- Atomic operations execute without interference from other operations.
- Is
a += 1
an atomic operation?
- See java.util.concurrent.atomic package.
- Should a transfer or a GPA update be atomic?
- The Java keyword synchronized ensures that only 1 thread may
access an object at any time.
- Bank account: getBalance, setBalance, deposit, withdraw
- Is this too restrictive?
Conflicts
- Operations are read or write.
- Conflicts occur if the order in which operations are executed impacts
the result.
- Write/write and read/write are conflicting operations.
- Read/read is OK.
Locking
- Retrieve a lock on a resource before accessing it.
- Rules:
- if requested lock is read and (no lock held or read lock held):
grant read lock
- if requested lock is read and write lock held: wait
- if requested lock is write and no lock held: grant write lock
- if requested lock is write and (read lock held or write lock held):
wait
- Java's wait/notify enable threads to communicate. wait blocks
waiting for a lock. notify releases a lock and notifies all those waiting
for the lock.
Models for XML DB Thread Safety
- Synchronize all - the most restrictive model that does not allow
concurrent reads.
- Copy update - make changes to a copy of the DB and replace old with an
(atomic) assignment. Keeps two copies of DB in memory during update.
- Locking
Sami Rollins
Wednesday, 21-Jan-2009 11:29:33 PST