Concurrency 2: electric boogaloo

Cards (10)

  • read x read = fine, read x write or write x write = not fine
  • verifying the write operation is possible is still part of the write operation
  • synchronized: synchronised methods can only run one at a time per object instance, so they are not thread safe
  • Synchronised blocking: use a generated lockObject, synchronise it, and use it as cross-thread synchronisation (the if test tests the value of the the lockObject instance) eg: lockObject is used to make sure list of connected clients (active read operations), which the write operation waits to be 0 before proceeding. Also need to notify from clientList class once is 0
  • Using synchronised, use a counter/tracker to keep track of 'live' read operations (increment at beginning, decrement once completely done), then write waits for this to be 0
  • Synchronised lock approach is weak to lockout - if there are many reads that keep going, write simply never reaches it's turn
  • Shallow cloning - get copy of all the data and read that instead
  • Locks - a library that simplifies these things. Surround read with lock.readlock.(lock/unlock)() and lock.writeLock.(lock/unlock)()
  • Create lock object with ReadWriteLock
  • if a thread does not notify all, other threads may simply wait forever and nothing else happen